0

I need to filter my custom post type "events" by Taxonomy Meta Field "city". But I can't make it work and need some help?

This is what I already have. But no results...

function aa_pre_get_posts($query)   {
    if (is_admin() || $query->get('post_type') != 'my-events') return;

    if(isset($_GET['city']) and trim($_GET['city']))
    {
        $query->set(
            'tax_query',
            array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'my_location',
                    'orderby' => 'meta_value',
                        'meta_query' => [[
                        'key' => 'city',
                        'value' => sanitize_text_field($_GET['city']),
                        'compare' => 'LIKE'
                    ]], 
                )               
            )
        );
    }
}
add_action('pre_get_posts', 'aa_pre_get_posts');

So, what's wrong?

1 Answer 1

3

This isn't possible via WP_Query, but more importantly, this would be stupendously expensive. Taxonomy queries are expensive. Post meta queries are super super expensive. Like queries are also expensive.

Finding all posts that have a term that has a meta that is like X, is an astoundingly expensive/slow query. Writing this out as a custom SQL query is the only option, and I would be surprised if the query finished in under 10 seconds. It could easily bring down a database server if your site gets a moderate or higher level of concurrent traffic.

Would it not be orders of magnitude faster and simpler to have a city taxonomy? If you need to filter or search for posts by something, use taxonomy terms. Meta is not for searching

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.