如何用现代活动日历精简版按DAT显示即将到来的活动

时间:2019-06-01 作者:Gregory

我正在使用插件Modern Events Calendar Lite.

这是一个最近的插件,所以实际上不存在文档。

所以我想在主页上显示我即将举办的活动。但我无法使其工作,因为start存储在第二个表中。

所以我有一个wpquery,它列出了自定义帖子“mec事件”,这没问题。但我无法将信息加入第二个表以获取start\\u date&;按其价值排序帖子。

我为您添加第二个表格wp mec日期和;事件存储在wp mec事件中

enter image description here

根据这首短裙:Filtering the JOIN tables in WP_Query示例代码:

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;
}
我想这样做:

<?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
                    }
                }
                    ?>
但我的var\\u dump()返回“null”。所以我可能做错了什么。任何帮助都将是非常感谢的!

2 个回复
最合适的回答,由SO网友:Gregory 整理而成

我找到了一个解决方案,进行自定义SQL查询

下面是代码(使用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();

SO网友:Darko Mitrovic
$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\'
        ),
    ),
);