I need to search custom post type by tagids. I have created a post type that is “gampu”. In this post type i have register 5 tags taxonomy like ‘fitting’, ‘adapter’, ‘type’, ‘volume’ and ‘gaki’. Now admin can create many tags in this tag taxonomies and when admin create any post then they will select the tags from all of those 5 register tag taxonomies.
On front end i want to add filter by tag. When user select (checkboxes) tags and click on search i need to fetch only those post that associated by tag ids. I have tried below code but its not working for me.
$args = array(
'post_type' => 'gampu',
'posts_per_page' => -1,
'tag__in' => array(30,31,34),
'post_status' => 'any',
'orderby' => 'ID',
'order' => 'DESC'
);
$query = new WP_Query($args);
echo '<pre>'; var_dump($query->request); echo '</pre>';
$getpost = get_posts($args);
echo '<pre>'; var_dump($getpost); echo '</pre>';
Nothing is coming from this code. Fix me if i am wrong anywhere in above code.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I have done by my self. Here is the solution
$args = array(
'post_type' => 'gampu',
'posts_per_page' => -1,
'post_status' => 'any',
'orderby' => 'ID',
'order' => 'DESC',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'my-tag-taxonomy1',
'field' => 'id',
'terms' => array(30,31,34),
),
array(
'taxonomy' => 'my-tag-taxonomy2',
'field' => 'id',
'terms' => array(30,31,34),
),
),
);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0