On my website, there are a couple of custom post types with default blog posts. On the blog posts, there are many categories.
One of the post types is webinar
. I like to query all posts from the post type webinar
and those blog posts that have category id 43
I tried following way but does not work.
1:
$query = new WP_Query(array(
'post_type' => array('webinar'), // I need all post from webinar
'cat' => '43', // I need all blog post under this category (Post type post category id 43)
);
Result: No post found
2:
$query = new WP_Query(array(
'post_type' => array('webinar','post'), // I need all post from webinar
'cat' => '43', // I need all blog post under this category (Post type post category id 43)
);
Result: Only posts of category id 43, no post from post types webinar
I need something like the following:
$query = new WP_Query(array(
array(
'relation' => 'OR',
array(
'post_type' => array('webinar')
),
array(
'post_type' => 'post',
'cat' => '43'
)
)
));
How can I do this?
3
Answers
Would something like this work? I have not tested this so may need playing with a bit.
If you dont want to use get_posts then can always use new WP_Query but will need to ammend
Solution 1
NOTE
This will work only in case all
webinar
posts will be assigned to at least 1{webinar_tax_name}
term.Solution 2
You may use
posts_request
hook and update SQL for 1 of your queries in your example. But I would prefer 1st solution as it will be more clear for other devs.