【问题标题】:Sort wp_query by meta value按元值对 wp_query 进行排序
【发布时间】:2013-08-15 20:29:28
【问题描述】:

我有一个自定义帖子类型,即投资组合,我需要能够按元值对其进行排序,元值是作者姓名 ::: 我已经尝试了几个示例,但没有一个工作 ::: 任何帮助将不胜感激: ::

我的代码

$args = array(
    "post_type" => "portfolio",
    "meta_key" => "authors_name",
    "orderby" => "meta_value",
    "order" => "ASC"
);

$custom_query = new WP_Query( $args );

这也不起作用

$args = array(
    "post_type" => "portfolio",
    "meta_key" => "authors_name",
        'meta_query' => array(
            array(
                'key' => 'authors_name',
            ),
            ),
            'orderby' => 'meta_value',
            'order' => 'ASC',
);

【问题讨论】:

    标签: wordpress sorting custom-post-type meta


    【解决方案1】:

    我能够使用 add_filter('pre_get_posts' ::: 对这个问题进行排序:本质上这就是我的脚本现在的样子 :::

    function laudes_order( $wp_query ) {
    
            $wp_query->set('meta_key', 'authors_name');
            $wp_query->set('orderby', 'meta_value');
            $wp_query->set('order', 'DESC');
    
    }
    
    add_filter('pre_get_posts', 'laudes_order');
    
    
    $args = array(
        "post_type" => "portfolio",
    );
    
    
    $custom_query = new WP_Query( $args );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-04
      • 1970-01-01
      • 2015-01-12
      相关资源
      最近更新 更多