WordPress query using category__and with tax_query

I’ve been searching for a while for a method that will accomplish usage of category__and while using a custom taxonomy for those categories.

I have a custom post type of “project”, inside that project I have used a custom taxonomy “portfolio” to categorize the projects.

Read More

If I use a standard query like this (Nothing comes of it):

                $args = array(
                    'post_type' => 'project',
                    'posts_per_page' => 12,
                    'orderby'=> 'title',
                    'order' => 'ASC',
                    'category' => $vcategory
            );  
            $loop = new WP_Query( $args );

I have attempted to use something like this as well:

                    $args = array(
                    'post_type' => 'project',
                    'posts_per_page' => 12,
                    'orderby'=> 'title',
                    'order' => 'ASC',
                    'tax_query' => array(
                        'taxonomy' => 'tagportfolio'
                    ),
                    'category__and' => array( $vcategory )
            );  
            $loop = new WP_Query( $args );

I have an app that allows people to sort posts by category using three dropdown boxes. box1 box2 box3

If a selection is made from box1 the posts on the page are updated to that category.

If then a selection is made from box2 the page would be updated with posts that contain the category from box1 && box2.

If after a selection is made from box3 the page would be updated with posts that contain the categories from box1 && box2 && box3.

This is why I was hoping to use category__and. I know I can query the taxonomy category using terms => (the terms), though this would not work for my needs since it would show posts from box1 that do not also contain the category from box2.

I am looking for a situation that follows && logic not ||.

Thank you.

Related posts

1 comment

  1. Categories are taxonomies, but they get special treatment because they are built-in. The category__and property will only work for the category taxonomy.

    For custom taxonomies, you can use the tax_query to query posts that have multiple terms in that taxonomy:

    'tax_query' => array(
        array(
            'taxonomy'  => 'tagportfolio',
            'field'     => 'term_id', //change to name or slug if necessary
            'terms'     => $vcategory,
            'operator'  => 'AND'
        )
    ),
    

    The above tax query should accomplish the same thing as category__and for your custom taxonomy.

Comments are closed.