【问题标题】:Page ordering by title in WP Admin is not workingWP Admin 中按标题排序的页面不起作用
【发布时间】:2014-09-25 23:54:59
【问题描述】:

我想在 Wordpress 管理员的自定义帖子类型的自定义元字段中生成复选框列表。我正在使用 ACF 创建字段。我可以查询所有页面,但 orderby 参数不受尊重。它只是按 date_created 输出它们。

这是我的查询:

$pageArgs = array(
    'post_type' => 'page',
    'posts_per_page' => -1,
    'nopaging' => true,
    'orderby' => 'title',
    'order' => 'ASC'
  );
  $pageArray = array();
  $pageQuery = new WP_Query($pageArgs);
  if($pageQuery->have_posts()) {
    while($pageQuery->have_posts()) {
      $pageQuery->the_post();
      $pageArray[get_the_permalink()] = get_the_title();
    }
  }

您可以看到我创建了一个空数组,并在循环期间将查询中的值输入到数组中。如果我在循环后转储数组,则不会反映 orderby 参数。此外,如果我转储查询对象,它会显示 SQL 查询,并且仍然按 menu_order 排序。

$pageQuery 转储:

    ...
    public 'request' => string 'SELECT SQL_CALC_FOUND_ROWS  wp_8_posts.ID 
    FROM wp_8_posts  
    WHERE 1=1  AND wp_8_posts.post_type = 'page' AND ((wp_8_posts.post_status = 'publish'))  
    ORDER BY wp_8_posts.menu_order, wp_8_posts.post_date DESC LIMIT 0, 10'
    ...

这是因为查询是在管理员中进行的吗?一旦循环完成,我当然可以对数组进行排序,但为了更好地理解 WP,我想知道为什么会这样。

另外,我可以查询它,它会正确列出页面......

$args = array(
    'authors'      => '',
    'child_of'     => 0,
    'date_format'  => get_option('date_format'),
    'depth'        => 0,
    'echo'         => 1,
    'exclude'      => '',
    'include'      => '',
    'link_after'   => '',
    'link_before'  => '',
    'post_type'    => 'page',
    'post_status'  => 'publish',
    'show_date'    => '',
    'sort_column'  => 'post_title',
    'title_li'     => __('Pages'), 
    'walker'       => ''
  );
  var_dump(wp_list_pages( $args ));

以字母顺序转储页面名称。

【问题讨论】:

    标签: php advanced-custom-fields wordpress


    【解决方案1】:

    这是两个错误的混合。

    我的查询以及 ACF 文档都非常错误。应该是

    'post_type' => 'page', 
    'posts_per_page' => -1, 
    'orderby' => 'meta_value', 
    'meta_key' => 'title', 
    'order' => 'ASC' 
    

    ACF 文档说应该是'meta_key_num',但这对我不起作用。我可以使用 'meta_value' 按自定义 ACF 字段排序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      • 2014-10-21
      • 1970-01-01
      • 1970-01-01
      • 2023-01-19
      相关资源
      最近更新 更多