【问题标题】:Wordpress pre_get_posts not changing the queryWordpress pre_get_posts 不更改查询
【发布时间】:2017-06-12 22:50:06
【问题描述】:

我正在尝试使用 pre_get_posts 修改查询,以便仅在存档页面上显示提供学生折扣的学校。有问题的字段称为“student_discount”,是一个复选框,如果为真则返回 1。

当我使用 get_post_custom 来回显值时,它是“_student_discount: Array”。

这是我目前拥有的:

function show_discounts( $query ) {
   if( !is_admin() && $query->is_main_query()  && is_post_type_archive( 'job_listing' ) ) {
    $query->set( 'meta_query', array(        
        array(
              'key' => 'student_discount',
              'value' => 1,
              'compare' => '='
        ),
  ));       
}
add_action( 'pre_get_posts', 'show_discounts' );

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    试试看。

    改变这个:

    is_post_type_archive( 'job_listing' )
    

    为此:

    $query->query_vars['post_type'] === 'job_listing' )
    

    结果:

    function show_discounts( $query ) {
       if( !is_admin() && $query->is_main_query() && $query->query_vars['post_type'] === 'job_listing' ) {
        $query->set( 'meta_query', array(        
            array(
                  'key' => 'student_discount',
                  'value' => 1,
                  'compare' => '='
            ),
      ));       
    }
    add_action( 'pre_get_posts', 'show_discounts' );
    

    如果你能够让它工作,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-09
      相关资源
      最近更新 更多