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 badges1 Answer
Reset to default 0You 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);
}