【问题标题】:Cannot order destinations alphabetically无法按字母顺序排列目的地
【发布时间】:2017-07-13 09:51:35
【问题描述】:

我有未排序的目的地,我按以下方式打印:

$destinations = get_posts( array(
                            'post_type' => 'destination_showcase',
                            'posts_per_page' => -1,
                            'post_status' => 'publish',
                            'meta_query' => array(
                                array(
                                    'key' => 'destination_state',
                                    'value' => ':"'.$state_id . '";' , // looking for serialized value in quotes
                                    'compare' => 'LIKE'
                                )
                            ),
                        ) );

我想按字母顺序打印目的地。当我对#destinations 数组进行 var_dump 编辑时,我得到了所有目的地及其参数。我想得到他们的标题,即“post_title”并按字母顺序打印。这个我试过了,还是不行:

'orderby'=> $destinations->post_title, 'order' => 'ASC',

有什么想法可以完成这项任务吗?

【问题讨论】:

  • 不应该是'orderby'=> 'post_title'吗?

标签: php wordpress alphabetical


【解决方案1】:

试试这个

$destinations = get_posts( array(
    'post_type' => 'destination_showcase',
    'posts_per_page' => -1,
    'post_status' => 'publish',
    'orderby' => 'title',
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'key' => 'destination_state',
            'value' => ':"'.$state_id . '";' , // looking for serialized value in quotes
            'compare' => 'LIKE'
        )
    ),
) );

【讨论】:

    【解决方案2】:

    如果我正确阅读了WP Query manual,这应该可以工作:

      get_posts( array(
                            'post_type' => 'destination_showcase',
                            'posts_per_page' => -1,
                            'post_status' => 'publish',
                            'meta_query' => array(
                                array(
                                    'key' => 'destination_state',
                                    'value' => ':"'.$state_id . '";' , 
                                    'compare' => 'LIKE'
                                )
    
                            ),
                          'orderby' => 'title',
                          'order'   => 'ASC',
                        ) );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多