多个帖子类型的Wordpress查询

时间:2013-11-21 15:48:18

标签: wordpress custom-post-type

我完全被这个难过了。下面的代码允许我查询多个帖子类型。由于使用了类别,我将它们分解为这样。奇怪的是,我只从post_type ='post'获得帖子。我使用post__in的最终查询来建立ID所需的帖子。如果我打印出$ post_ids,我会得到我正在寻找的确切ID。但我的最终查询没有给我这些ID。思考??

$postArgs = array(
    'post_type' => 'post',
    'cat' => '16,17,18',
    'posts_per_page' => 5,
    'orderby' => 'date',
    'order' => 'DESC',
    'post_status' => 'publish'
);

$videoArgs = array(
    'post_type' => 'occ-videos',
    'posts_per_page' => 5,
    'orderby' => 'date',
    'order' => 'DESC',
    'post_status' => 'publish'
);

$photoArgs = array(
    'post_type' => 'occ-photography',
    'posts_per_page' => 5,
    'orderby' => 'date',
    'order' => 'DESC',
    'post_status' => 'publish'
);



$docArgs = array(
    'post_type' => 'wpfb_filepage',
    'posts_per_page' => 5,
    'orderby' => 'date',
    'order' => 'DESC',
    'post_status' => 'publish'
);


$posts_query = get_posts($postArgs);
$docs_query = get_posts($docArgs);
$video_query = get_posts($videoArgs);
$photo_query = get_posts($photoArgs);


// start putting the contents in the new object
$all_posts = array_merge($posts_query, $docs_query, $video_query, $photo_query);

$post_ids = wp_list_pluck( $all_posts, 'ID' );//Just get IDs from post objects

print_r($post_ids);


$artArgs = array(
    'posts_per_page' => 20,
    'post_status' => 'publish',
    'orderby' => 'post__in',
    'post__in' => $post_ids);


$artQuery = get_posts($artArgs);

1 个答案:

答案 0 :(得分:1)

我的理解是Wordpress总是默认为post的post_type。因此,它只查找具有其中一个ID的帖子 - 并忽略您的自定义帖子类型。

尝试在$artArgs

中添加一行
$artArgs = array(
  'post_type' => array('post','page','occ-videos','occ-photography'), //Add this line
  'posts_per_page' => 20,
  'post_status' => 'publish',
  'orderby' => 'post__in',
  'post__in' => $post_ids
);

并添加您需要Wordpress查询的任何帖子类型。