通过带有PRE_GET_POST的自定义元排除帖子

时间:2014-04-25 作者:Nasgor

我正在为活动日历创建自定义帖子。我的自定义帖子有两个自定义元字段,称为“event\\u star\\u date”&;“event\\u end\\u date”。我想使用pre_get_posts 排除其“event\\u end\\u date”小于todays date或“event\\u end\\u date”为空且“event\\u start\\u date”小于todays date的事件。我看到了一些代码,但最终对我不起作用。

多亏了@cybnet,我已经很快得到了我需要的代码,但钢铁无法工作。我想nedd可能是这样的:

function bbwp_calendar_visible_events( $query ) {
if ( !is_admin() && $query->is_main_query() ){
    if ( is_post_type_archive( $postType ) ) {
            $meta_query = array(
            \'relation\' => \'OR\',
            array(
                \'relation\' => \'AND\',
                array(
                    \'key\'   => \'event_start_date\',
                    \'value\' => current_time( \'timestamp\' ),
                    \'type\' => \'CHAR\',
                    \'compare\' => \'>=\',
                ),
                array(
                    \'key\'   => \'event_end_date\',
                    \'value\' => \'\',
                    \'type\' => \'CHAR\',
                    \'compare\' => \'=\',
                ),
            ),
            array(
                \'key\'   => \'event_end_date\',
                \'value\' => current_time( \'timestamp\' ),
                \'type\' => \'CHAR\',
                \'compare\' => \'>=\',
            ),
        );

        $query->set( \'meta_query\', $meta_query );
    }
}
}

add_action( \'pre_get_posts\', \'bbwp_calendar_visible_events\' );
感谢每一个人

1 个回复
SO网友:cybmeta

您可以使用查询的“meta\\u query”参数,我认为这比过滤post_where:

add_action( \'pre_get_posts\', \'bbwp_calendar_visible_events\' );
function bbwp_calendar_visible_events( $query ) {

    $postType = \'bbwp_calendar\';

    if ( !is_admin() && $query->is_main_query() ){

        if ( is_post_type_archive( $postType ) ) {

            $meta_query = array(
                              \'relation\' => \'OR\',
                              //current time <= event_end_date
                              array(
                                  \'key\'   => \'event_end_date\',
                                   //Check https://codex.wordpress.org/Function_Reference/current_time
                                   //to return the current time (today) in the same format
                                   //you store the date in your custom field
                                  \'value\' => current_time( \'timestamp\' ),
                                  \'type\' => \'DATETIME\',
                                  \'compare\' => \'<=\',
                              ),
                              //event_end_date = \'\'
                              array(
                                  \'key\'   => \'event_end_date\',
                                   //Check https://codex.wordpress.org/Function_Reference/current_time
                                   //to return the current time (today) in the same format
                                   //you store the date in your custom field
                                  \'value\' => "",
                                  \'type\' => \'DATETIME\',
                                  \'compare\' => \'=\',
                              ),
                     );

            $query->set( \'meta_query\', $meta_query );

         }
     }
} 
更多信息:

结束

相关推荐

为什么不调用/触发“Plugins_Load”?

我正在打电话load_plugin_textdomain 然而,一旦加载了插件,就不会发生这种情况。我确实激活了一个插件,所以这不应该触发吗?add_action(\"plugins_loaded\", \"test_override\"); function init_localization() { echo \"init_localization<br>\"; load_plugin_textdomain (&#x