'WP_Query by an array or list of post slugs

Is this possible, I couldn't find the question asked anywhere on the web.

If you had a list of posts by their slug, example:

$postslugs = array("page-one","page-two","page-three"); 

And you wanted to create a single query to find all these specific pages:

$args=array(  
'post__not_in' => array($post->ID),  
'posts_per_page'=> -1,
'post_type' =>      'customtype',
'post_status' =>    'publish',
'pagename' => $postslugs
);

$my_query = new wp_query( $args ); 

The result should come back with the three pages. This of course would not work the way it's written now. Is this possible?



Solution 1:[1]

I would like to notice, that there is some issue with old WordPress versions where post_name it is not = to a post_slug. For example if you try to create new post with title Customer.io it will create a post with name customer-io but slug will be customerio the same will be for this Pokéapi The same will be if you change post slug after you have created the post. So the best way is to check manually inside loop of your WP_Query.

$desired_array_of_slugs = array( 'page-one', 'page-two', 'page-three' ); 

$query_args = array(  
    'posts_per_page' => -1,
    'post_type'      => 'customtype',
    'post_status'    => 'publish'
);

$my_query = new WP_Query( $query_args );

Then

foreach($my_query as $post) {
    if(in_array(sanitize_title($post->name)], $desired_array_of_slugs, true)){
        //do ur staff
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fdrv