用于搜索的WP_QUERY中的条件帖子

时间:2016-03-24 作者:Branson Werner

我有一个自定义的帖子类型,我试图限制搜索结果。

CPT是一个交易/优惠券,每个交易都有一个开始和结束日期。

我只是想在搜索中显示这些帖子:

对于非经常性事件。。。。显示结束日期为今天或今天之后的帖子

OR

<对于经常性事件,将其限制为仅显示结束日期介于今天之间且不超过7天的事件这些查询中的每一个都能独立完美地工作,但我无法将它们组合在一起。。。

function search_pre_get_posts( $query ) {

    if ( $query->is_search() && $query->is_main_query()   ) {  
        $recurring_count = am_get_recurring_count($post->ID);
        // Show non-recurring events with end date of today or after
        if ($recurring_count == 0){
            $currentdate =  date(\'Y-m-d\');
            $query->set( 
                \'meta_query\',
                array(
                    \'relation\' => \'OR\',
                    array(
                         \'key\' => \'am_enddate\',
                         \'compare\' => \'>=\',
                         \'value\' => $currentdate,
                    ),
                    array(
                         \'key\' => \'am_enddate\',
                         \'compare\' => \'NOT EXISTS\',
                    )
                )
            );
            $query->set( \'orderby\', \'meta_value\' );
            $query->set( \'order\', \'ASC\' );
        } else {
           // Show recurring events with end date of today or after, that doesnt exceed 7 days.
            $currentdate =  date(\'Y-m-d\');
            $enddate = strtotime(date(\'Y-m-d\') . "+7 days");
            $enddate = date(\'Y-m-d\',$enddate);
            $query->set( 
                \'meta_query\',
                array(
                    \'relation\' => \'OR\',
                    array(
                         \'key\' => \'am_enddate\',
                         \'compare\' => \'NOT EXISTS\',
                    ),
                    array(
                        \'relation\' => \'AND\',
                        array(
                             \'key\' => \'am_enddate\',
                             \'compare\' => \'>=\',
                             \'value\' => $currentdate,
                        ),
                        array(
                             \'key\' => \'am_enddate\',
                             \'compare\' => \'<=\',
                             \'value\' => $enddate,
                        )
                    )
                )
            );
            $query->set( \'orderby\', \'meta_value\' );
            $query->set( \'order\', \'ASC\' );
        }
    }
}
add_action( \'pre_get_posts\', \'search_pre_get_posts\' ); 

2 个回复
SO网友:Zachary Eagle

删除if语句,并将查询组合成如下内容如何:

\'meta_query\',
array(
\'relation\' => \'OR\',
    array(
        array(
            \'relation\' => \'AND\',
                array(
                    \'key\' => \'am_get_recurring_count\',
                    \'compare\' => \'=\',
                    \'value\' => \'0\',
                ),
                array(
                    \'relation\' => \'OR\',
                    array(
                         \'key\' => \'am_enddate\',
                         \'compare\' => \'>=\',
                         \'value\' => $currentdate,
                    ),
                    array(
                         \'key\' => \'am_enddate\',
                         \'compare\' => \'NOT EXISTS\',
                        )
                )
        )
    )
    array(
        array(
            \'relation\' => \'AND\',
                array(
                    \'key\' => \'am_get_recurring_count\',
                    \'compare\' => \'!=\',
                    \'value\' => \'0\',
                ),
                array(
                    \'relation\' => \'OR\',
                    array(
                         \'key\' => \'am_enddate\',
                         \'compare\' => \'NOT EXISTS\',
                    ),
                    array(
                        \'relation\' => \'AND\',
                        array(
                             \'key\' => \'am_enddate\',
                             \'compare\' => \'>=\',
                             \'value\' => $currentdate,
                        ),
                        array(
                             \'key\' => \'am_enddate\',
                             \'compare\' => \'<=\',
                             \'value\' => $enddate,
                        )
                    )
                )
        )

   )

 )
还没有机会测试这个。请让我知道它是否有效。

SO网友:Sofiane Achouba

Try this

function search_pre_get_posts( $query ) {

    if ( $query->is_search() && $query->is_main_query()   ) {  
            $currentdate =  date(\'Y-m-d\');
            $enddate = strtotime(date(\'Y-m-d\') . "+7 days");
            $enddate = date(\'Y-m-d\',$enddate);

            $query->set( 
                \'meta_query\',
                array(
                    \'relation\' => \'OR\',
                    array(
                         \'key\' => \'am_enddate\',
                         \'compare\' => \'NOT EXISTS\',
                    ),
                    array(
                        \'relation\' => \'OR\',
                        array(
                            \'key\' => \'am_enddate\',
                            \'compare\' => \'>=\',
                            \'value\' => $currentdate,
                        ),
                        array(
                            \'key\' => \'am_recurrence_id\',
                            \'compare\' => \'NOT EXISTS\',
                        )
                    ),
                    array(
                        \'relation\' => \'AND\',
                        array(
                             \'key\' => \'am_enddate\',
                             \'compare\' => \'>=\',
                             \'value\' => $currentdate,
                        ),
                        array(
                             \'key\' => \'am_enddate\',
                             \'compare\' => \'<=\',
                             \'value\' => $enddate,
                        ),
                        array(
                            \'key\' => \'am_recurrence_id\',
                            \'compare\' => \'EXISTS\',
                        )
                    )
                )
            );
            $query->set( \'orderby\', \'meta_value\' );
            $query->set( \'order\', \'ASC\' );
    }
}
add_action( \'pre_get_posts\', \'search_pre_get_posts\' ); 

相关推荐

将wp_Query替换为wp_User_Query

我在插件中制作了一些订阅者档案和单曲(个人资料页),其中我还包括了一个“用户单曲”模板,通过template_include. 不过,我正在尝试从插件中删除一些模板,以使其使用主题模板。我用了locate_template( \'single.php\' ) 从活动主题中选择单个模板。我没有使用全局wp_query 在本例中显示我的内容,但页面显示了基于查询默认值的循环(十篇帖子)。我想知道的是,我是否可以完全放弃默认查询,用wp_user_query 我可以将查询到的用户ID输入其中。然后我想筛选the