简体   繁体   English

按类别或作者wordpress获取帖子

[英]get posts by category or author wordpress

I want to get posts from a particular category or if the post by a particular user . 我想从特定类别或特定用户的帖子中获取帖子。 It seems we could only have AND condition in wordpress . 看来我们只能在wordpress中使用AND条件。 I know the below code is wrong but this is what I need to get - I want all the posts written by a particular user OR all the posts from a particular category 我知道下面的代码是错误的,但这是我需要得到的 - 我想要特定用户写的所有帖子或特定类别的所有帖子

$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 );

Please do help. 请帮忙。 Thanks in advance . 提前致谢 。

Try this code 试试这个代码

$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)

If you want both do get_posts() 2 times. 如果你想要两个都做get_posts() 2次。

  1. all posts from user 来自用户的所有帖子
  2. all posts from the category and then merge both arrays with array_merge() 来自该类别的所有帖子,然后使用array_merge()合并两个数组

I have found solution to this ,I have done it with a custom query 我找到了解决方案,我已经使用自定义查询完成了

 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

where $cat_list is an array with your category ids and $authors is an array with your author ids . 其中$ cat_list是一个包含类别ID的数组,$ authors是一个包含您的作者ID的数组。 Hope this would help someone in need. 希望这会帮助有需要的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM