1

I'm using the plugin Modern Events Calendar Lite.

This is a recent plugin, so actually no doc exist.

So i'm looking a way to display my upcoming events on homepage. But i can not make it to work, because start is stored in a second table.

So i have a wpquery that list custom posts "mec-events", this is ok. But i'm not able to JOIN info to second table to get start_date & to order posts on its value.

I add you a capture of the second table wp-mec-dates & events are store in wp-mec-events

enter image description here

Based on this tuto : Filtering the JOIN tables in WP_Query exemple code :

add_filter( 'posts_join', 'add_other_table', 10, 2 );

/**
 * Joining another table and relating the column post_id with the Post's ID
 *
 * @param string $join String containing joins.
 * @param WP_Query $wp_query Object.
 * @return string
 */
function add_other_table( $join, $wp_query ) {
 global $wpdb;
 $join .= " JOIN {$wpdb->prefix}my_table as mytable on mytable.post_id = {$wpdb->posts}.ID ";
 return $join;
}

i figure to do like this :

<?php
    $args = array(
        'post_type'=> 'mec-events',
        'orderby' => 'dstart',
        'order'    => 'ASC',
    );              

    add_filter( 'posts_join', 'add_other_table', 10, 2 );
    function add_other_table( $join, $upcoming_events_query ) {
        global $wpdb;
        $join .= " JOIN {$wpdb->prefix}mec_dates as wp_mec_dates on wp_mec_dates.post_id = {$wpdb->posts}.ID ";
        return $join;
    }   

    $upcoming_events_query = new WP_Query( $args );
    if($upcoming_events_query->have_posts() ) {
        while ( $upcoming_events_query->have_posts() ) {
            $upcoming_events_query->the_post();

            $image_url = get_the_post_thumbnail_url('','liste-etablissements'); 
            $event_cities = get_field('contact_city',$post->ID);
            $start_date = get_field('dstart');
            var_dump($start_date);
?>
    <div class="presta-img-home-events">
        <?php if($image_url[0]) { ?>
            <img src="<?php echo esc_url( $image_url ); ?>" alt="<?php the_title_attribute(); ?>">
        <?php } else { ?>
            <img src="<?php echo get_template_directory_uri() . '/images/placeholder-blog.jpg'; ?>" alt="<?php the_title_attribute(); ?>">
        <?php } ?>
        <div class="presta-img-home-events-overlay"><?php //echo $start_date; ?> - <?php if($event_cities) { $cities = 0; $max_cities = 1; foreach($event_cities as $event_city) { $cities++; if($cities > $max_cities) { break; } echo ''. get_the_title( $event_city->ID ) .''; } } ?></div>
    </div>
    <div class="featured-content-wrapper">
        <h3 class="featured-title"> <a title="<?php the_title_attribute(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <!--<a href="<?php the_permalink(); ?>" class="single_add_to_wishlist" ><?php esc_html_e('Découvrir','BeProvence'); ?><i class="fa fa-heart"></i></a>-->
    </div><!-- featured content wrapper -->
                    <?php
                    }
                }
                    ?>

But my var_dump() return "null". So i probably do something wrong. Any help will be apreciate !

2 Answers 2

1

i have found a solution, making a custom SQL query

here is the code ( using WPML )

        $startday = date("Y-m-d");
        if ( defined( 'ICL_LANGUAGE_CODE' ) ) {
          $lang = ICL_LANGUAGE_CODE;
        }           
        //echo $startday;
        global $wpdb,$post;
        $results = $wpdb->get_results( "SELECT * FROM wp_posts, `wp_mec_dates` AS mecd, wp_icl_translations WHERE wp_posts.ID = mecd.post_id and post_status='publish' AND wp_icl_translations.language_code='$lang' AND dstart>'$startday' and  wp_posts.ID = wp_icl_translations.element_id ORDER BY dstart" );
        foreach ($results as $post) {
            setup_postdata($post);
            $event_permalink = get_the_permalink();
            $event_date = $post->dstart; 
            $new_event_date = date("d/m", strtotime($event_date));
            $event_title = get_the_title();
            echo $new_event_date . ' - <a href="'.$event_permalink.'" title="'.$event_title.'">' . substr($event_title,0,38) .'</a><br />';
        }
        wp_reset_postdata();
0
$args = array( 
    'posts_per_page' => 5, 
    'post_type' => 'mec-events', 
    'orderby' => 'mec_start_date', 
    'order' => 'ASC',
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key'     => 'mec_start_date',
            'value'   => date('Ymd'),
            'compare' => '>=',
            'type' => 'DATE'
        ),
    ),
); 
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Feb 19, 2022 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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