1

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.

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);
        }
| improve this answer | |
  • This didn't work for me @Michelle :( - I've updated the post to show what the next calls is dependent which is $posts = $post_data['posts'];. - So get_posts_by_category, returns a 'posts' array. – Dave May 20 at 16:01
  • Ah, didn't notice you were using posts_per_page, that should be numberposts. See updated answer. @Dave – Michelle May 20 at 16:14
  • Also @Dave I didn't test $post_data['posts'] but you can see what's in the $post_data array using var_dump($post_data); after the if/else call. – Michelle May 20 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.