0

Since we're not skinning or using the standard template for this, we've ran into some difficulties but we're at a point we're too far along in the project to turn back. I've never done a WP_Query this fancy but two things to outline how it's structured.

  1. I'm querying the post type tribe_events where postmeta keys are equal to _VenueCity, _VenueState, and _VenueCountry and values are equal to the GET/POST request.
  2. The postmeta is assigned to the tribe_venue taxonomy which makes it hard for me to query the postmeta directly since it's not directly attached to the tribe_events post type.

I've tried many things but this is what remains of my many different efforts.

<?php
    // Support for multiple locations to be filtered.
    $request_cities = array_map(function($item){return utf8_decode(urldecode($item));}, explode('+', $_REQUEST['city']));
    $request_states = array_map(function($item){return utf8_decode(urldecode($item));}, explode('+', $_REQUEST['state']));
    $request_countries = array_map(function($item){return utf8_decode(urldecode($item));}, explode('+', $_REQUEST['country']));

    // If the event only has 1 item convert to string.
    if (is_array($request_cities) && count($request_cities) <= 1) {
        $request_cities = implode("|", $request_cities);
    }
    if (is_array($request_states) && count($request_states) <= 1) {
        $request_states = implode("|", $request_states);
    }
    if (is_array($request_countries) && count($request_countries) <= 1) {
        $request_countries = implode("|", $request_countries);
    }
    $event_args = [];
    $event_args['meta_query'] = [
        [
            'key' => '_VenueCity',
            'value' => $request_cities
        ], [
            'key' => '_VenueState',
            'value' => $request_states
        ], [
            'key' => '_VenueCountry',
            'value' => $request_countries
        ],
        'relation' => 'AND'
    ];
    $event_args['tax_query'] = [
        [
            'taxonomy' => 'tribe_venue',
            'field' => 'term_id',
            'terms' => $request_cities
        ]
    ];
    $events = tribe_get_events($event_args);
?>

If someone can help me figure this out I'd be beyond grateful at this point because I've spent so much time trying to figure this out.

3
  • have you tried putting meta query inside tax query? i can't find any documentation for this, just a guess
    – inarilo
    Aug 23, 2017 at 20:11
  • also, should field be term_id, or name / slug?
    – inarilo
    Aug 23, 2017 at 20:13
  • I just figured it out after tons of searching. Thank you for the help, though! Aug 23, 2017 at 20:18

1 Answer 1

0

So I found the solution after tons of searching from the events calendar's website and modified it to suit my needs. The code is as shown below: (Note I also removed the array to string conversion since it's pretty pointless.)

<?php
    // Support for multiple locations to be filtered.
    $request_cities = array_map(function($item){return utf8_decode(urldecode($item));}, explode('+', $_REQUEST['city']));
    $request_states = array_map(function($item){return utf8_decode(urldecode($item));}, explode('+', $_REQUEST['state']));
    $request_countries = array_map(function($item){return utf8_decode(urldecode($item));}, explode('+', $_REQUEST['country']));

    //Retrieve venues that match query criteria
    $args = [
        'nopaging' => true,
        'post_type' => 'tribe_venue',
        'meta_query' => [
            [
                'key' => '_VenueCity',
                'value' => $request_cities,
                'compare' => 'IN',
            ], [
                    'key' => '_VenueState',
                    'value' => $request_states,
                    'compare' => 'IN',
            ], [
                    'key' => '_VenueCountry',
                    'value' => $request_countries,
                    'compare' => 'IN',
            ],
            'relation' => 'AND'
        ]
    ];
    $venues = get_posts($args);
    $venue_ids = wp_list_pluck($venues, 'ID');
    wp_reset_postdata();

    $event_args[] = [
        'meta_query' => [
            [
                'key' => '_EventVenueID',
                'value' => $venue_ids,
                'compare' => 'IN',
            ]
        ],
        'paged' => $paged
    ];
    $events = tribe_get_events($event_args);
?>

Source: https://theeventscalendar.com/support/forums/topic/events-list-with-filter-by-venue-in-custom-template/#post-1290489

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.