Wordpress in_category和has_tag条件在WP_Query中不起作用

时间:2018-04-23 18:24:02

标签: php wordpress validation woocommerce conditional

我想阻止在会员订阅(使用Woocommerce会员资格)之前创建的Wordpress文章(这是一个在线杂志)

我需要检查一篇文章是否属于特定类别,标签而不是执行“if”。 in_categoryhas_tag不起作用,到目前为止我一直使用is_category,但它仅在类别存档页面上阻止文章。问题是文章需要被阻止到根目录,因为它们可以出现在网站上的其他地方,用于前“相关文章”。 甚至is_tag也可以,但只能在标签存档页面上运行。

以下是我的代码:

add_action( 'pre_get_posts', 'date_search_filter' );

function date_search_filter($query) {
$user_id = get_current_user_id();
global $wpdb;
$startdate=$wpdb->get_col("select tmpm.meta_value from tmwp_posts as tmpp inner join tmwp_postmeta as tmpm on tmpp.ID = tmpm.post_id where tmpm.meta_key = '_start_date' and tmpp.post_author = ". $user_id . " order by tmpm.meta_id desc limit 1",0);



if ( !is_admin() && !current_user_can('administrator') && $query->is_main_query() ) {
    if ($query->is_category('387')){

$query->set( 'date_query', 
    [    [
    'after'     => $startdate[0],
    'inclusive' => true,
    ]    ] 
    );
    }
  }
}

我要检查的代码是 if ($query->is_category('387')){ 我不想使用is_single()因为它会阻止每篇文章,甚至包括我拥有的产品。

0 个答案:

没有答案