0

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.

1 Answer 1

0

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),
        ),
    ),
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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