MEC Query - Repeating Events not showing up

I created a WP Query for my looper provider to show all the events for the Modern Event Calendar but it is not showing all the repeating events, all my events repeat and it is only showing 1 of each. I also need to show the date of the event and so far I have only been able to show the initial date not the date of the repeated event. one of my events is every Wednesday at 8pm, and the first date was Jan 4th, but the next event that is >= to today should be July 19 but I am not seeing that date.

Note: I followed this post among others that have been mentioned for setting up loopers and a MEC query: MEC Events Looper Questions

Here is my Query:

$meta_query[] = array(
    'key'     => 'mec_start_date',
    'value'   => date('Ymd'),
    'compare' => '>=',
    'type' => 'DATE'
);
$query = array(
    'post_type'  => 'mec-events',
    'meta_key'   => 'mec_start_date',
    'orderby'    => 'meta_value',
    'order'      => 'ASC',
    'meta_query' => $meta_query
);
var_dump( http_build_query( $query ) );

And the query with the date updated to be dynamic using {{dc:global:date format="dFY"}}:
post_type=mec-events&meta_key=mec_start_date&orderby=meta_value&order=ASC&meta_query%5B0%5D%5Bkey%5D=mec_start_date&meta_query%5B0%5D%5Bvalue%5D={{dc:global:date format="dFY"}}&meta_query%5B0%5D%5Bcompare%5D=%3E%3D&meta_query%5B0%5D%5Btype%5D=DATE

What am I doing wrong with my query and how can I get the date to reflect the repeated event date instead of the first date the event happened?

Hello Scott,

Thanks for writing in!

Your query argument should be this:

$meta_query[] = array(
    'relation' => 'AND',
    array(
        'key'     => 'mec_start_date',
        'value'   => date('Ymd'),
        'compare' => '>=',
        'type' => 'DATE'
    ),
    array(
        'key'     => 'mec_repeat_status',
        'value'   => 1,
        'compare' => '=='
    )
    
);
$query = array(
    'post_type'  => 'mec-events',
    'meta_key'   => 'mec_start_date',
    'orderby'    => 'meta_value',
    'order'      => 'ASC',
    'meta_query' => $meta_query
);
var_dump( http_build_query( $query ) );

You will have to query for events that start today or that has is a repeating event.

Best Regards.

I updated the query string as you suggested but I am seeing 2 issues, first nothing shows up at all when I use the query string (cleared cache a few times), and second in your screenshot the dates show the wrong date, it is showing the date of the first event when it was setup, these repeat weekly so they should be dates of today and the future. I am using this query string with the dynamic date updated:
post_type=mec-events&meta_key=mec_start_date&orderby=meta_value&order=ASC&meta_query%5B0%5D%5Brelation%5D=AND&meta_query%5B0%5D%5B0%5D%5Bkey%5D=mec_start_date&meta_query%5B0%5D%5B0%5D%5Bvalue%5D={{dc:global:date format="dFY"}}&meta_query%5B0%5D%5B0%5D%5Bcompare%5D=%3E%3D&meta_query%5B0%5D%5B0%5D%5Btype%5D=DATE&meta_query%5B0%5D%5B1%5D%5Bkey%5D=mec_repeat_status&meta_query%5B0%5D%5B1%5D%5Bvalue%5D=1&meta_query%5B0%5D%5B1%5D%5Bcompare%5D=%3D%3D

I am using this dynamic code for the start date: {{dc:post:meta key="mec_start_date" type="date" format="M j"}} but also not seeing basics like {{dc:post:featured_image}} or {{dc:post:title}} spit anything out for me (the title actually does show “Home” but that is not correct).

Also my Debug code only comes up with this:
Index: -1 Current Data: array(0) { }

Any idea why this is not working at all? Thanks for the help!

Hello Scott,

The relationship must be changed to OR.
'relation' => 'OR',

The date is shown may be incorrect because you have used {{dc:post:meta key="mec_start_date" type="date" format="M j"}}. Be advised that you have created the event on that particular date and set it to repeat weekly. You will need to display the upcoming event date using different dynamic content.

Best Regards.

Thanks for the help, my events are displaying now.

Do you have any ideas what the Dynamic code would be to show the repeating date? Or maybe there is a way to show all the content the looper provides kind of like the {{dc:looper:debug_consumer}} code, it shows some tings but not everything, is there something like that I can use to see what all is available, the entire array that is being spit out?

Hello Scott,

The event items were linked to their respective even item posts. Temporarily remove the link first so that you can click on the debug icon and see the data for each event item.

Best Regards.

Yes, I know about the debug, I was asking if there is something that shows more because I am not seeing things like the repeating date, or even featured image in the debug code. Is there some other way to find out that the dynamic code for the repeating date is or some way to print out everything the looper has access to?

Hey Scott,

We don’t have a way that shows more. And, though we provide support for MEC, it is only up to its features and not its data structure nor how to access its data.

I was afraid you would say something like that. Well I suggest you put in a feature request so the debugger can be modified to show everything the looper has access to kind of like using var_dump in PHP, maybe add a key modifier or something to the Dynamic Content code.

For anyone that is in the same situation I figure out some code that will spit out all the MEC event info and since all my dates are repeating it works for my needs. I may be excluding some columns/fields but you can test the MYSQL section in something like PHPMyAdmin and just fix the table/column/filed names to match your table names (fi you changed your “wp_” prefix or need more data) and add today’s date just for testing. Also my code pulls all the “Custom Fields” and spits them out as “mec_fields_1”, “mec_fields_2”, “mec_fields_3”… and so on.

Place this code in Functions.php and then setup a custom Looper Provider and use the name uptournaments or rename uptournaments in the function cs_looper_custom_uptournaments to whatever you want:

// Function to parse the serialized mec_fields data and extract the keys and values
function parse_mec_fields($data) {
    $parsed_data = [];

    // Unserialize the data
    $unserialized_data = unserialize($data);

    if (is_array($unserialized_data)) {
        foreach ($unserialized_data as $key => $value) {
            $parsed_data['mec_fields_' . $key] = $value;
        }
    } else {
        // If the data is not serialized, assume it's a single value
        $parsed_data['mec_fields'] = $data;
    }

    return $parsed_data;
}

// The function to fetch events and parse mec_fields
add_filter('cs_looper_custom_uptournaments', function ($result) {
    $current_date = date('Ymd');
    global $wpdb;

    // Fetch events that are today or have repeating date equal to today or in the future
    $query = $wpdb->prepare("
        SELECT p.ID, p.post_title, p.post_content, p.post_name, pm.*, 
        d.*, FROM_UNIXTIME(d.tstart) AS start_datetime,
        p2.guid AS featured_image,
        MAX(CASE WHEN pm3.meta_key = 'mec_fields' THEN pm3.meta_value END) AS mec_fields
        FROM {$wpdb->posts} AS p
        INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id
        LEFT JOIN {$wpdb->prefix}mec_dates AS d ON p.ID = d.post_id
        LEFT JOIN {$wpdb->postmeta} AS pm3 ON p.ID = pm3.post_id AND pm3.meta_key LIKE 'mec_fields%'
        LEFT JOIN {$wpdb->postmeta} AS pm2 ON p.ID = pm2.post_id AND pm2.meta_key = '_thumbnail_id'
        LEFT JOIN {$wpdb->posts} AS p2 ON pm2.meta_value = p2.ID
        WHERE d.dstart >= %s AND p.post_type = 'mec-events' AND p.post_status = 'publish'
        GROUP BY d.ID
        ORDER BY d.dstart ASC, d.tstart ASC
    ", $current_date);

    // Run the custom query
    $results = $wpdb->get_results($query, ARRAY_A);

    // Parse the serialized mec_fields data for each result
    foreach ($results as &$result) {
        if (isset($result['mec_fields'])) {
            $parsed_fields = parse_mec_fields($result['mec_fields']);
            $result = array_merge($result, $parsed_fields);
            unset($result['mec_fields']);
        }
    }

    return $results;
}, 10, 2);

Here is a the MySQL cleaned up so you can run it in something like PHPMyAdmin… Change 20230726 (YYYYMMDD) to today’s date:

SELECT p.ID, p.post_title, p.post_content, p.post_name, pm.*, 
    d.*, FROM_UNIXTIME(d.tstart) AS start_datetime,
    p2.guid AS featured_image,
    MAX(CASE WHEN pm3.meta_key = 'mec_fields' THEN pm3.meta_value END) AS mec_fields
    FROM wp_posts AS p
    INNER JOIN wp_postmeta AS pm ON p.ID = pm.post_id
    LEFT JOIN wp_mec_dates AS d ON p.ID = d.post_id
    LEFT JOIN wp_postmeta AS pm3 ON p.ID = pm3.post_id AND pm3.meta_key LIKE 'mec_fields%'
    LEFT JOIN wp_postmeta AS pm2 ON p.ID = pm2.post_id AND pm2.meta_key = '_thumbnail_id'
    LEFT JOIN wp_posts AS p2 ON pm2.meta_value = p2.ID
    WHERE d.dstart >= 20230726 AND p.post_type = 'mec-events' AND p.post_status = 'publish'
    GROUP BY d.ID
    ORDER BY d.dstart ASC, d.tstart ASC

Here are some of the Dynamic codes I am using (My code takes the repeating start date/time and turns it into a format the looper can understand and renames it “start_datetime”):

{{dc:looper:field key="featured_image"}}
{{dc:looper:field key="post_title"}}
{{dc:looper:field key="start_datetime" type="time" format="l gA"}}
{{dc:looper:field key="start_datetime" type="date" format="M j"}}
{{dc:looper:field key="mec_fields_1"}}
{{dc:looper:field key="mec_fields_2"}}
{{dc:looper:field key="mec_fields_3"}}
{{dc:looper:field key="mec_fields_4"}}

Thank you for sharing your solution. I’ll list this case as a feature request.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.