1

如果存在这样的键,我试图首先获取按数字 meta_key 排序的自定义帖子列表,如果没有键,则继续按标题排序。

我是说:

  1. AObject (object_order => 1)
  2. FObject (object_order => 2)
  3. BObject (object_order => 3)
  4. AObject (object_order => NULL)
  5. BObject (object_order => NULL)
  6. CObject (object_order => NULL) ... 等等。

我有这样的代码:

$properties_for_map = array(
    'post_type' => 'property',
    'posts_per_page' => -1,
    'meta_key' => 'object_order',
    'orderby' => 'meta_value_num title',
    'order' => 'ASC',
    'tax_query' => $tax_query
);

但它只显示带有元键的帖子。我试图在网站上找到答案,并找到了一些示例,但不太了解它们。也许有人可以帮助我并解释这种方法。

更新1: 可以这样做吗?

$properties_for_map = array(
    'post_type' => 'property',
    'posts_per_page' => -1,
        'meta_query'  => array(
    'relation' => 'OR',
    array(
        'key'     => object_order,
        'compare' => 'EXISTS',
    ),
    array(
        array(
            'key'   => object_order,
            'compare' => 'NOT EXISTS',
        ),
    ),
),
         'orderby' => array( 'meta_value_num' => 'ASC', 'title' => 'ASC' ),
    'tax_query' => $tax_query
);
4

1 回答 1

0

我能够获取我所有的帖子并首先使用元值对它们进行排序,然后按标题对没有元值的帖子的其余部分进行排序。

$args = array(
        'post_type'        => 'your-post-type',
        'posts_per_page'   => 20,
        'offset'           => 0,
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => 'your-custom-field-key',
                'compare' => 'EXISTS'
            ),
            array(
                'key' => 'your-custom-field-key',
                'compare' => 'NOT EXISTS'
            )
        ),
        'orderby' => 'meta_value_num title',
        'order' => 'ASC',
    );
于 2020-08-12T08:31:45.593 回答