Displaying several specific pages using WP_Query()

I want to display the pages ‘famous’ and ‘mschief’.

Using this code, WP_query() doesn’t return any result:

Read More
$args = array(
'post_type' => 'page',
'pagename' => 'famous,mschief',
'posts_per_page' => 3
);

This code returns one of the 2 pages:

$args = array(
'post_type' => 'page',
'pagename=famous,mschief',
);

And this code returns the 2 pages correctly:

$args = array(
'post_type' => 'page',
'pagename=famous,mschief',
'posts_per_page' => 3
);

Why does the first piece of code (the only correct one to me) doesn’t work? And why the two others produce these results?

Related posts

6 comments

  1. If you look at the query object after those queries are run, you’ll see that none of those examples are doing what you think they’re doing. The first issue is that you can’t mix query string and array parameters, and the second issue is that pagename can only be used to load a single page.

    In this one, you’re missing a closing quote after pagename. Assuming this is a typo, the problem is noted above – it’s looking for a single page with slug famousmschief

    $args = array(
    'post_type' => 'page',
    'pagename => 'famous,mschief',
    'posts_per_page' => 3
    );
    

    In this one, pagename is being ignored and it’s just querying for the most recent page using the default posts per page value, which would appear to be 1.

    $args = array(
    'post_type' => 'page',
    'pagename=famous,mschief',
    );
    

    In this one, same issue as last, but you’re loading more pages, so it’s just loading the three most recent pages:

    $args = array(
    'post_type' => 'page',
    'pagename=famous,mschief',
    'posts_per_page' => 3
    );
    

    If you want to query multiple pages, use the post__in argument, and the page IDs:

    $args = array(
        'post_type' => 'page',
        'post__in' => array( 23,42 )
    );
    
  2. The post_name__in attribute

    As of WordPress 4.4 we can use the post_name__in attribute of the WP_Query class. See the trac ticket #33065.

    Example:

    $args = [
        'post_name__in'      => ['famous', 'mschief'],
        'post_type'          => 'page',
       'ignore_sticky_posts' => 1,
    ];
    
  3. For the first one, you have mismatched quotes on the third line. What it seems you were trying to do was:

    'pagename' => 'famous,mschief',
    
  4. You can write your own function, using a posts_where filter

    function page_names_query( $n = array() ) {
      static $pages;
      if ( is_null($pages) && ! empty($n) && is_array($n) ) {
        $pages = array_filter( array_map('sanitize_title', $n) );
        if ( ! empty($pages) ) {
          // using this function itself to filter next query
          add_filter('posts_where', __FUNCTION__, 999 ); 
          return new WP_Query('post_type=page'); // return the filtered query
        } else { // no valid page names passed
          $pages = NULL;
          return new WP_Query('p=-1'); // return a WP_Query with no posts
        }
      } elseif( current_filter() === 'posts_where' && ! empty($pages) && is_string($n) ) {
        $w = ' AND post_name IN (';
        global $wpdb;
        foreach ( $pages as $page ) { $w .= $wpdb->prepare('%s,', $page); }
        $pages = NULL; // reset static variable
        remove_filter('posts_where', __FUNCTION__, 999 ); // remove filter
        return $n . rtrim($w, ',') . ')';
      }
    }
    

    after that you can use it like so

    $query =  page_names_query( array('famous','mschief') ); // return a WP_Query
    
    if ( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post();
    
    // your loop here
    
    endwhile; endif;
    wp_reset_post_data();
    
  5. Your question needs clarification. I guess you are referring to 2 posts rather than 2 pages.

    You can use this PHP code in your child themes functions.php file to display posts by post i.d’s.

    add_action( 'pre_get_posts', 'wpsites_display_specific_posts_home_page' );
    
    function wpsites_display_specific_posts_home_page( $query ) {
    
    if( $query->is_main_query() && $query->is_home() ) {
    
        $query->set( 'post__in', array( 007, 008 ) );
    
        }
    }
    

    This code will only display posts with the i.d’s 007 and 008 in your home page loop. If you’re using a static from page, this code will work on your posts page according to your reading settings.

    Using i.d’s is better as they remain the same even when you change the slug.

  6. add category to page with:

    function wpc_cat_pages() {
        register_taxonomy_for_object_type('category', 'page');
    }
    add_action('init', 'wpc_cat_pages');
    

    and display it with:

    $args = array(
            'post_type' => 'page',
            'category' => 'categori',
            'posts_per_page' => 'number post'
    );
    

Comments are closed.