本文介绍了根据自定义日期字段隐藏过去的事件帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望根据我设置的自定义日期字段隐藏过去的事件。

I wish to hide past events based on the custom date field I set up.

<?php 
the_post();
// Get 'events' posts
$events_posts = get_posts( array(
'post_type' => 'events',
'posts_per_page' => -4, // Unlimited posts
'orderby' => 'meta_value',
'meta_key' => 'event_date',
'order' => 'ASC'
) );

if ( $events_posts ):
?>

此代码当前按顺序显示我的事件,但是我想隐藏早于今天的事件?

This code is currently showing my events in order, but I want to hide events older than today's date?

推荐答案

如果要按自定义帖子字段过滤数据,则必须使用为此

If you want to filter data by custom post field you have to use meta_query for this

工作示例:

$args = [
    'post_type' => 'events',
    'posts_per_page' => -1, // Unlimited posts
    'orderby' => 'meta_value',
    'meta_key' => 'event_date',
    'order' => 'ASC',
    'meta_query' => [
        'relation' => 'AND',
        [
            'key' => 'event_date',
            'value' => date('Y-m-d'), //<-- replace this with your correct date format
            'compare' => '>',
            'type' => 'DATE'
        ],
    ],
];

$queryEvent = new WP_Query($args);
if ($queryEvent->have_posts()) :
    /* Start the Loop */
    while ($queryEvent->have_posts()) : 
    $queryEvent->the_post();

    //you post

    endwhile;
endif;

希望这会有所帮助!

相关答案:

这篇关于根据自定义日期字段隐藏过去的事件帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-09 22:37