最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Grab posts by multiple categories

programmeradmin1浏览0评论

So I have the below code that works great:

if (isset($meta_field['category'])) {
    $post_data = get_posts_by_category($meta_field['category'], 0,
        $post_limit);
} else {
    $post_data = get_posts_by_category('1', 0, $post_limit);
}

// Extract posts
$posts = $post_data['posts'];

Where in the else statement, I'm calling to pull in all posts tagged the 1 category which is working great.


Here is what I'd like to achieve - So I'm attempting to pull posts from two different categories in the else statement, but I can't seem to make it work correctly.

Here is my attempt:

        if (isset($meta_field['category'])) {
            // Get all post data for that category
            $post_data = get_posts_by_category($meta_field['category'], 0,
                $post_limit);
        } else {
            $args = [
                'posts_per_page' => $post_limit,
                'offset'=> 1,
                'category' => [
                    '9', '3'
                ]
            ];
            $post_data = get_posts($args);
        }

I'd like to pull in all posts from my categories tagged 9 and 3, but I'm getting no results back - but when I call get_posts_by_category, I in fact do get results back.

So I have the below code that works great:

if (isset($meta_field['category'])) {
    $post_data = get_posts_by_category($meta_field['category'], 0,
        $post_limit);
} else {
    $post_data = get_posts_by_category('1', 0, $post_limit);
}

// Extract posts
$posts = $post_data['posts'];

Where in the else statement, I'm calling to pull in all posts tagged the 1 category which is working great.


Here is what I'd like to achieve - So I'm attempting to pull posts from two different categories in the else statement, but I can't seem to make it work correctly.

Here is my attempt:

        if (isset($meta_field['category'])) {
            // Get all post data for that category
            $post_data = get_posts_by_category($meta_field['category'], 0,
                $post_limit);
        } else {
            $args = [
                'posts_per_page' => $post_limit,
                'offset'=> 1,
                'category' => [
                    '9', '3'
                ]
            ];
            $post_data = get_posts($args);
        }

I'd like to pull in all posts from my categories tagged 9 and 3, but I'm getting no results back - but when I call get_posts_by_category, I in fact do get results back.

Share Improve this question edited May 20, 2020 at 16:00 Dave asked May 20, 2020 at 15:50 DaveDave 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You should be able to use this:

        if (isset($meta_field['category'])) {
            // Get all post data for that category
            $post_data = get_posts_by_category($meta_field['category'], 0,
                $post_limit);
        } else {
            $args = array(
              'numberposts' => $post_limit,
              'offset'=> 1,
              'category' => '9,3',
            );
            $post_data = get_posts($args);
        }
发布评论

评论列表(0)

  1. 暂无评论