【问题标题】:wordpress - orderby query by "type" is not working?wordpress - “类型”的orderby查询不起作用?
【发布时间】:2014-12-10 17:11:18
【问题描述】:

我正在尝试以特定的帖子类型顺序查询列表,但它不起作用。

$args = array(              
                'post_type' => array('a','b', 'c', 'd', 'e'),
                'tax_query' => array(
                        array(
                            'taxonomy' => 'category',
                            'field' => 'slug',
                            'terms' =>  ''.$term_list.''
                            ),
                        ),
                'orderby' => 'type'

                );

怎么了?它适用于 'rand' 和其他查询字符串,但 'type' 似乎还不够!

谢谢!

【问题讨论】:

  • 从 4.0 版开始可以按帖子类型排序,也许您使用的是旧 WP 版本。

标签: php sql-order-by wordpress custom-type


【解决方案1】:

我完成这项工作的唯一方法是过滤 pre_get_posts。我还没有深入研究核心以真正了解设置的内容,但过滤器确实有效。

function my_search_filter( $query ) {

    if( $query->is_search() ) {
        $query->set( 'post_type', array('post', 'page','case-study','office', 'career', 'event') );
        $query->set( 'orderby', 'type');
    }
}
add_filter( 'pre_get_posts', 'my_search_filter' );

【讨论】:

  • 感谢您的帮助 :) 我如何通过我的模板文件使用此过滤器,因此根据 tpl 我有不同的类型顺序?
  • 您使用的是哪个模板/WordPress 功能?
  • 我正在使用 wp_query 函数。
  • array('a','b','c','d','e'), 'tax_query' => 数组(array('taxonomy' => 'category', 'field' => 'slug', 'terms' => ''.$term_list.'' ), ), 'orderby' => 'type' ); $the_query = new WP_Query($args); while ($the_query -> have_posts()) : $the_query -> the_post();` ?>
  • 从头开始主题:)
【解决方案2】:

这听起来像是自定义帖子类型的单一视图。你可以这样做:

function my_single_filter( $query ) {

    if( $query->is_singular('custom_post_type_slug') ) {
        $query->set( 'post_type', array('a','b','c','d','e') );
        $query->set( 'orderby', array( 
        array( 'taxonomy' => 'category', 
               'field' => 'slug', 
               'terms' => ''.$term_list.'' ), 
        ));
        $query->set( 'orderby', 'type');
    }
}
add_filter( 'pre_get_posts', 'my_single_filter' );

这将进入您的functions.php 文件。您必须通过 functions.php 获取 $term_list 变量的数据。在您的 single.php 文件中,您可以放置​​默认循环。

【讨论】:

  • 非常感谢,但我不能让它工作,这真的很奇怪......如何将我的函数解析为我的 tpl 文件?我试图将一个数组放入 orderby,也不起作用:(
猜你喜欢
  • 2018-12-28
  • 2018-06-12
  • 2017-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多