0

I would like to use both the meta_query and the tax_query, but using the following I can get results for the meta_query. What am I doing wrong?

$wp_query = new WP_Query( array(
            'post_type'     => 'whatson',
            'post_status'   => 'publish',
            'meta_query'    => array(

                array(
                    'key'     => 'sc_related_venues_title',
                    'value'   => $location,
                    'compare' => 'LIKE'  ,
                    'field'   => 'title',
                ),
            ),

             'tax_query' => array(
                array(
                    'taxonomy'  => 'eventtype',
                    'terms'     => $event_type,
                    'field'     => 'slug',
                    'compare'   => 'LIKE'         
                ),
           ),

            'posts_per_page' => '10',
            'paged' => $paged
        ) );
  • 2
    Keep in mind that meta_query is extremely slow, and scales very poorly as the number of posts and post meta increase. tax_query is significantly faster, and your related venues title shouldn't be a custom field, it should have been a custom taxonomy – Tom J Nowell Nov 19 '17 at 12:20
3

Would you believe it, as soon as I posted this I remembered I asked a question about a query last year. I had a look over it and I've adapted my new query like so:

$category_slug = filter_input(
    INPUT_GET,
    'eventtype',
    FILTER_SANITIZE_STRING
);

$cat_query = [];
if( $event_type ){
    $cat_query = [
        [
            'taxonomy' => 'eventtype',
            'field'    => 'slug',
            'terms'    => $event_type

        ]
    ];
}

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
    'post_type'      => array('whatson'),
    'post_status'  => 'publish',
    'tax_query'  => $cat_query,
    'meta_query' => array(

        array(
            'key' => 'sc_related_venues_title',
            'value' => $location,
            'compare' => 'LIKE'  ,
            'field'     => 'title',
        ),
    ),
    'orderby'        => 'date',
    'order'          => 'desc',
    'posts_per_page' => '10',
    'paged'          => $paged,
);
$wp_query = new WP_Query( $args);
| improve this answer | |
0

Try after removing 'compare' => 'LIKE' from tax_query.

tax_query doesn't have the param compare it's available for meta_query

'tax_query' => array(
   array(
      'taxonomy'  => 'eventtype',
      'terms'     => $event_type,
      'field'     => 'slug',
   )
),
| improve this answer | |
0

Since you are already using slug to compare your taxonomy. Try with

$wp_query = new WP_Query( array(
        'post_type'     => 'whatson',
        'post_status'   => 'publish',
        'eventtype'     => $event_type,
        'meta_query'    => array(
            array(
                'key'     => 'sc_related_venues_title',
                'value'   => $location,
                'compare' => 'LIKE'  ,
                'field'   => 'title',
            ),
        ),
        'posts_per_page' => '10',
        'paged' => $paged
    ) );
| improve this answer | |
-1

try belwo code for meta query and tax_query.

<?php
$category_object=get_queried_object();
$term_id = $category_object->term_id;
$name = $category_object->name;
$description = $category_object->description;

$wp_query = new WP_Query( array(
    'post_type'     => 'whatson',
    'post_status'   => 'publish',
    'meta_query' => array(
                    'relation' => 'AND',
                    array(
                            'key' => 'sc_related_venues_title',
                            'value' => $location,
                            'compare' => 'LIKE',
                            'field'   => 'title',
                    ),
                ),
    'tax_query'  => array(
            array(
                'taxonomy' => 'eventtype',
                'field' => 'id',
                'terms' => $term_id
            )
        ),
    'posts_per_page' => '10',
    'paged' => $paged
) );
?>
| improve this answer | |
  • Could you please edit your answer using the edit button above and add in a bit more explanation of what you did differently from the question to solve the issue? Code snippets with explanations tend to be more helpful to future readers. Thanks! – Howdy_McGee Sep 12 '17 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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