I want to display the pages ‘famous’ and ‘mschief’.
Using this code, WP_query() doesn’t return any result:
$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?
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 slugfamousmschief
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.In this one, same issue as last, but you’re loading more pages, so it’s just loading the three most recent pages:
If you want to query multiple pages, use the
post__in
argument, and the page IDs:The
post_name__in
attributeAs of WordPress 4.4 we can use the
post_name__in
attribute of theWP_Query
class. See the trac ticket #33065.Example:
For the first one, you have mismatched quotes on the third line. What it seems you were trying to do was:
You can write your own function, using a
posts_where
filterafter that you can use it like so
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.
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.
add category to page with:
and display it with: