0

I am working on a Wordpress theme i.e. Directory engine. It is having a filtering functionality in the header based on taxonomy term slug. I just want to know from where this tax_query is generated for WP_Query.Here is the code.

 $this->add_action('pre_get_posts', 'pre_get_places');
 function pre_get_places($query) {
        global $current_user;

        if ($query->is_main_query() && is_author()) {
            $query->set('post_type', $this->post_type);
            if ($current_user->user_login == $query->query['author_name']) {
                $query->set('post_status', array(
                    'pending',
                    'reject',
                    'archive',
                    'draft',
                    'publish'
                ));
            }
        }
        // set default post type query in place category, location listing
        if ($query->is_main_query() && (is_tax('place_category') || is_tax('location') || is_tax('age_from') || is_tax('place_type') || is_tax('daddy_lounge') || is_tax('place_caregivers'))) {
            $query->set('post_type', $this->post_type);
            $query->set('post_status', 'publish');
        }

        /**
         * is main query and cach request showposts
         */
        if (isset($_REQUEST['showposts']) && $query->is_main_query()) {
            $query->set('showposts', $_REQUEST['showposts']);
        }

        /**
         * if is post type archive set status to publish
         */
        if (is_post_type_archive($this->post_type) && !is_admin()) {
            if (!isset($query->query_vars['post_status'])) {
                $query->set('post_status', 'publish');
            }
        }

        /**
         * filter orderby
         */
        if (isset($_REQUEST['sortby']) 
            && ( $query->is_main_query() || (isset($query->query_vars['meta_key']) && $query->query_vars['meta_key'] == 'rating_score') )
        ) {
            if ($_REQUEST['sortby'] !== 'date') {
                // $query->query_vars['meta_key'] = $_REQUEST['sortby'];
                $query->set('orderby', 'meta_value_num');
            } else {

                // order by date
                $query->set('orderby', 'date');
            }

            // order desc
            $query->set('order', 'DESC');
        }
        // order by rating score
        if(isset($_REQUEST['query']['orderby']) && $_REQUEST['query']['orderby'] == 'rating_score') {
            $query->set('orderby', 'meta_value_num date');
            if( !isset($_REQUEST['query']['meta_key'] ) ) {
                $query->query_vars['meta_key'] = $_REQUEST['query']['orderby'];
                $query->meta_query = array(
                     //check to see if et_featured has been filled out
                    'relation' => 'OR',
                    array(
                         //check to see if date has been filled out
                        'key' => $_REQUEST['query']['orderby'],
                        'compare' => 'BETWEEN',
                        'value' => array(
                            0,
                            5
                        )
                    ),
                    array(
                         //if no et_featured has been added show these posts too
                        'key' => $_REQUEST['query']['orderby'],
                        'value' => '',
                        'compare' => 'NOT EXISTS'
                    )
                );
            }else{
                // order by rating score for feature block
                if($_REQUEST['query']['meta_key'] == 'et_featured') {
                    $query->set('orderby', 'menu_order');
                    $query->set('meta_query' , array(
                        array(
                            'key' => 'rating_score',
                            // 'value' => array(0, 5),
                            // 'compare' => 'BETWEEN'
                        ),
                        array(
                            'key' => 'et_featured',
                            'value' => '1',
                            'compare' => 'LIKE'
                        )
                    )); 
                }
                if($_REQUEST['query']['meta_key'] == 'de_event_post') {
                    // order by rating score for event block
                    $query->set('orderby', 'menu_order');
                    $query->set('meta_query' , array(
                        array(
                            'key' => 'rating_score',
                            'value' => array(0, 5),
                            'compare' => 'BETWEEN'
                        ),
                        array(
                            'key' => 'de_event_post',
                            'value' => '',
                            'compare' => '!=', 
                            'type' => 'NUMERIC'
                        )
                    )); 
                }            
            }
        }

        $is_search = ! (empty($_REQUEST['query']) || empty($_REQUEST['query']['s']));
        if ((et_load_mobile() && !is_single() ) && !$is_search) {
            if (!is_author() && isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == $this->post_type) {
                if(!is_search()){
                    $query->set('meta_key', 'et_featured'); 
                }
                if(!isset($query->query_vars['near_lat']) || $query->query_vars['near_lat'] == '' ){
                    $query->set('orderby', 'meta_value_num date');
                }
                if (isset($query->query_vars['meta_value'])) {
                    unset($query->query_vars['meta_value']);
                }                
                $query->meta_query = array(
                     //check to see if et_featured has been filled out
                    'relation' => 'OR',
                    array(
                         //check to see if date has been filled out
                        'key' => 'et_featured',
                        'compare' => 'IN',
                        'value' => array(
                            0,
                            1
                        )
                    ) ,
                    array(
                         //if no et_featured has been added show these posts too
                        'key' => 'et_featured',
                        'value' => 1,
                        'compare' => 'NOT EXISTS'
                    )
                );
            }
        }

        return $query;
    }

 $args = array(
    "post_type" => "place",
    "post_status" => "publish",
    "orderby" => "date",
    "place_category" => "",
    "location" => "",
    "showposts" => "4",
    "order" => "DESC",
    "meta_key" => "et_featured",
    "meta_value" => "1",
    "paginate" => "page",
    "age_from" => "2",
    "age_to" => "5",
    "place_caregivers" => "no",
    "place_type" => "indoor",
    "daddy_lounge" => "no",
    "paged" => "1",
    "thumbnail" => "big_post_thumbnail",
    "posts_per_page" => false
};
$query = new WP_Query($args);

$query returns

"data":null,
"paginate":"<script type=\"application\/json\" class=\"ae_query\">{\"post_type\":\"place \",\"post_status\":\"publish\",\"orderby\":\"date\",\"place_category\":\"\",\"location\":\"\",\"showposts \":\"4\",\"order\":\"DESC\",\"meta_key\":\"et_featured\",\"meta_value\":\"1\",\"paginate\":\"page\", \"age_from\":\"2\",\"age_to\":\"5\",\"place_caregivers \":\"no\",\"place_type\":\"indoor\",\"daddy_lounge\":\"no\",\"paged\":\"1\",\"thumbnail\":\"big_post_thumbnail \",\"posts_per_page\":false}<\/script>",
"msg":"Successs",
"success":true,
"max_num_pages":null,
"status":"0 place",
"total":0,
"total_status":{

    "publish":"71",
    "pending":0,
    "archive":"5",
    "reject":"1",
    "draft":"1"

},
"data_query":{

    "query":{
        "query":{
            "post_type":"place",
            "post_status":"publish",
            "orderby":"date",
            "place_category":"",
            "location":"",
            "showposts":"4",
            "order":"DESC",
            "meta_key":"et_featured",
            "meta_value":"1",
            "paginate":"page",
            "age_from":"2",
            "age_to":"5",
            "place_caregivers":"no",
            "place_type":"indoor",
            "daddy_lounge":"no",
            "paged":"1",
            "thumbnail":"big_post_thumbnail",
            "posts_per_page":false
        },
        "query_vars":{
            "post_type":"place",
            "post_status":"publish",
            "orderby":"date",
            "place_category":"",
            "location":"",
            "showposts":4,
            "order":"DESC",
            "meta_key":"et_featured",
            "meta_value":"1",
            "paginate":"page",
            "age_from":"2",
            "age_to":"5",
            "place_caregivers":"no",
            "place_type":"indoor",
            "daddy_lounge":"no",
            "paged":1,
            "thumbnail":"big_post_thumbnail",
            "posts_per_page":4,
            "error":"",
            "m":"",
            "p":0,
            "post_parent":"",
            "subpost":"",
            "subpost_id":"",
            "attachment":"",
            "attachment_id":0,
            "name":"",
            "static":"",
            "pagename":"",
            "page_id":0,
            "second":"",
            "minute":"",
            "hour":"",
            "day":0,
            "monthnum":0,
            "year":0,
            "w":0,
            "category_name":"",
            "tag":"",
            "cat":"",
            "tag_id":"",
            "author":"",
            "author_name":"",
            "feed":"",
            "tb":"",
            "comments_popup":"",
            "preview":"",
            "s":"",
            "sentence":"",
            "fields":"",
            "menu_order":"",
            "category__in":[
            ],
            "category__not_in":[
            ],
            "category__and":[
            ],
            "post__in":[
            ],
            "post__not_in":[
            ],
            "tag__in":[
            ],
            "tag__not_in":[
            ],
            "tag__and":[
            ],
            "tag_slug__in":[
            ],
            "tag_slug__and":[
            ],
            "post_parent__in":[
            ],
            "post_parent__not_in":[
            ],
            "author__in":[
            ],
            "author__not_in":[
            ],
            "ignore_sticky_posts":false,
            "suppress_filters":false,
            "cache_results":true,
            "update_post_term_cache":true,
            "update_post_meta_cache":true,
            "nopaging":false,
            "comments_per_page":"50",
            "no_found_rows":false,
            "taxonomy":"daddy_lounge",
            "term":"no"
        },
        "tax_query":{
            "queries":[
                {
                    "taxonomy":"age_from",
                    "terms":"2",
                    "field":"slug",
                    "operator":"IN",
                    "include_children":true
                },
                {
                    "taxonomy":"age_to",
                    "terms":"5",
                    "field":"slug",
                    "operator":"IN",
                    "include_children":true
                },
                {
                    "taxonomy":"place_caregivers",
                    "terms":[
                        "no"
                    ],
                    "field":"slug",
                    "operator":"IN",
                    "include_children":true
                },
                {
                    "taxonomy":"place_type",
                    "terms":[
                        "indoor"
                    ],
                    "field":"slug",
                    "operator":"IN",
                    "include_children":true
                },
                {
                    "taxonomy":"daddy_lounge",
                    "terms":[
                        "no"
                    ],
                    "field":"slug",
                    "operator":"IN",
                    "include_children":true
                }
            ],
            "relation":"AND",
            "queried_terms":{
                "age_from":{
                    "terms":"2",
                    "field":"slug"
                },
                "age_to":{
                    "terms":"5",
                    "field":"slug"
                },
                "place_caregivers":{
                    "terms":[
                        "no"
                    ],
                    "field":"slug"
                },
                "place_type":{
                    "terms":[
                        "indoor"
                    ],
                    "field":"slug"
                },
                "daddy_lounge":{
                    "terms":[
                        "no"
                    ],
                    "field":"slug"
                }
            },
            "primary_table":"wp_posts",
            "primary_id_column":"ID"
        },
        "meta_query":{
            "queries":{
                "0":{
                    "key":"et_featured",
                    "value":"1"
                },
                "relation":"OR"
            },
            "relation":"AND",
            "meta_table":"wp_postmeta",
            "meta_id_column":"post_id",
            "primary_table":"wp_posts",
            "primary_id_column":"ID"
        },
        "date_query":false,
        "request":"SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id) INNER JOIN wp_term_relationships AS tt2 ON (wp_posts.ID = tt2 .object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( \n 0 = 1 \n AND \n 0 = 1 \n AND \n wp_term_relationships.term_taxonomy_id IN (130) \n AND \n tt1.term_taxonomy_id IN (125) \n AND \n tt2.term_taxonomy_id IN (128)\n) AND ( \n ( wp_postmeta.meta_key = 'et_featured' AND CAST(wp_postmeta.meta_value AS CHAR) = '1' )\n) AND wp_posts.post_type = 'place' AND ((wp_posts .post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 4",
        "posts":[
        ],
        "post_count":0,
        "current_post":-1,
        "in_the_loop":false,
        "comment_count":0,
        "current_comment":-1,
        "found_posts":0,
        "max_num_pages":0,
        "max_num_comment_pages":0,
        "is_single":false,
        "is_preview":false,
        "is_page":false,
        "is_archive":true,
        "is_date":false,
        "is_year":false,
        "is_month":false,
        "is_day":false,
        "is_time":false,
        "is_author":false,
        "is_category":false,
        "is_tag":false,
        "is_tax":true,
        "is_search":false,
        "is_feed":false,
        "is_comment_feed":false,
        "is_trackback":false,
        "is_home":false,
        "is_404":false,
        "is_comments_popup":false,
        "is_paged":false,
        "is_admin":true,
        "is_attachment":false,
        "is_singular":false,
        "is_robots":false,
        "is_posts_page":false,
        "is_post_type_archive":true,
        "thumbnails_cached":false
    }

actualy i want OR relation in "age_to" taxonomy and "age_from" taxonomy and want AND relation with other Taxonomies like "place_caregivers","place_type" and "daddy_lounge" but not able to find any way to edit tax_query. Any suggestions? Thanks in advance.

1 Answer 1

1

WordPress is generating the tax query for you - when you register a custom taxonomy, it also registers what's known as a query var. This is a "key"-type value that means you can simply do this:

new WP_Query([ 'age_from' => 2 ]);

... and WP will transform that to:

WP_Tax_Query([
    [
        'taxonomy' => 'age_from',
        'terms'    => 2,
        'field'    => 'slug',
    ]
]);

...but not before pre_get_posts fires (it happens on line 2802 of wp-includes/query.php). My suggestion would be to use a direct tax query in your arguments, rather than taxonomy query vars, so you have complete control over the relationship:

$args = array(
    'post_type'   => 'place',
    'post_status' => 'publish',
    'orderby'     => 'date',
    'tax_query'   => array(
        'relation' => 'AND',

        array(
            'relation' => 'OR',
            array(
                'taxonomy' => 'age_from',
                'terms'    => '2',
            ),
            array(
                'taxonomy' => 'age_to',
                'terms'    => '5',
            ),
        ),

        array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'place_caregivers',
                'terms'    => 'no',
            ),
            array(
                'taxonomy' => 'place_type',
                'terms'    => 'indoor',
            ),
            array(
                'taxonomy' => 'daddy_lounge',
                'terms'    => 'no',
            ),
        ),
    ),
);

Read about "nested" relations in WordPress 4.1

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.