按类别或作者wordpress获取帖子

时间:2015-07-10 11:25:45

标签: php wordpress

我想从特定类别或特定用户的帖子中获取帖子。看来我们只能在wordpress中使用AND条件。我知道下面的代码是错误的,但这是我需要得到的 - 我想要特定用户写的所有帖子或特定类别的所有帖子

$args = array(
    'posts_per_page'   => 10,
    'offset'           => $PageStart,
    'query' => array(
    'relation' => 'OR', /* <--                here */
    array(
        'author' => 18,
    ),
    array(
        'category' => 20,
        )
    ),
    'orderby'          => 'date',
    'order'            => 'DESC',
    'post_type'        => 'post',
    'post_status'      => 'publish',

);
//print_r($args);
$author_post = get_posts( $args );

请帮忙。提前谢谢。

3 个答案:

答案 0 :(得分:1)

试试此代码

$args = array(
    'posts_per_page'   => 10,
    'offset'           => $PageStart,
    'author'       => '18',
    'orderby'          => 'date',
    'order'            => 'DESC',
    'post_type'        => 'post',
    'post_status'      => 'publish',

);
$author_post = get_posts( $args );

$args = array(

    'posts_per_page'   => 10,
    'offset'           => $PageStart,
    'category'         => '20',
    'orderby'          => 'date',
    'order'            => 'DESC',
    'post_type'        => 'post',
    'post_status'      => 'publish',
);
$category_post = get_posts( $args );
$total_post = array_merge($category_post,$author_post)

答案 1 :(得分:0)

如果你想要两次get_posts()两次。

  1. 来自用户的所有帖子
  2. 该类别的所有帖子 然后使用array_merge()
  3. 合并两个数组

答案 2 :(得分:0)

我找到了解决方法,我已经使用自定义查询

完成了
 SELECT DISTINCT wposts.* 
            FROM $wpdb->posts wposts
                LEFT JOIN $wpdb->postmeta wpostmeta 
                ON wposts.ID = wpostmeta.post_id 
                LEFT JOIN $wpdb->term_relationships 
                ON (wposts.ID = $wpdb->term_relationships.object_id)
                LEFT JOIN $wpdb->term_taxonomy 
                ON ($wpdb->term_relationships.term_taxonomy_id 
                  = $wpdb->term_taxonomy.term_taxonomy_id)
                AND wposts.post_status = 'publish'
                AND $wpdb->term_taxonomy.taxonomy = 'category'
                AND ( $wpdb->term_taxonomy.term_id IN($cat_list) 
                OR wposts.post_author IN ($authors) )
            ORDER BY wposts.post_date DESC

其中$ cat_list是包含类别ID的数组,$ authors是包含您的作者ID的数组。希望这能帮助有需要的人。