Wordpress WP_Query call posts AND pages

2019-05-11 07:15发布

I have a feature slider set up that draws in posts that are tagged 'feature'

$my_query = new WP_Query(array(
  'showposts' => 3,
  'tag'  => 'feature' ));

Is is possible to draw in posts AND pages? I know you can draw pages with 'post_type'=>'page' but can you mix the two?

4条回答
冷血范
2楼-- · 2019-05-11 07:43

For anyone having to edit older code that doesn't use an array passed into WP_Query, you can add &post_type=any to get posts and pages (and other content). Unfortunately I don't see a way to get posts and pages (without other types) without using an array, since post_type would then require an array as the examples above show. However, this should be good enough if you are searching for a particular category anyway.

Example (this from vSlider v4.1.2 where &post_type=any is added so that pages are included in the slider):

$recent = new WP_Query($randimg."cat=".$options['imgCat']."&showposts=".$options['slideNr']."&post_type=any");

Thanks to @fivedigit and @my-jonny-wood for the answers above that led me to figuring this out and fixing the slider on my site!

查看更多
可以哭但决不认输i
3楼-- · 2019-05-11 07:55

You can specify an array value for the post_type parameter, as such:

$my_query = new WP_Query(array(
    'post_type' => array('post', 'page'),
    'tag'  => 'feature'
));

See this page for more info: WP Codex

查看更多
Juvenile、少年°
4楼-- · 2019-05-11 07:58

@fivedigit Thanks but I went with this in the end:

$my_query = new WP_Query(array(
    'post_type' => array('any'),
    'tag'  => 'feature'
));

Although your version may come in handy in the future!

查看更多
姐就是有狂的资本
5楼-- · 2019-05-11 08:02

I am wanting to create 4 featured posts on the front page, would I create an array, then select the one I want in each div from there?

查看更多
登录 后发表回答