Сортировка по настраиваемому meta_key в WordPress get_posts () и включение сообщений, у которых нет meta_key - PullRequest
0 голосов
/ 07 августа 2020

Я установил в своем посте WordPress настраиваемую метаданную под названием "my_sort", которая имеет числовые c значения, такие как 1, 2, 3, 4 ..... См. Изображение ниже:

enter image description here

I want to sort in ascending order by this meta "my_sort". This is the below code I am applying:

<?php 
    $catPost = get_posts(array('category' => get_cat_ID($categories[0]->name), 'meta_key' => 'my_sort', 'orderby' => 'meta_value_num', 'order' => 'ASC', 'numberposts' => 100)); //change this
  ?>

The problem with this code is that it is working but has one problem which I need to be fixed.

It is leaving all the other posts which do not have "my_sort" meta in them. I want to include those posts also. I want:

  1. First the posts that have "my_sort" must come in ascending order.
  2. Then the post that do not have "my_sort" meta should come.

Update

When I try the below query.

$catPost = get_posts( 
        array (
            'category' => get_cat_ID($categories[0]->name), 
            'numberposts' => 100, 
            'orderby' => 'meta_value_num', 
            'meta_type' => 'NUMERIC',
            'order' => 'ASC', 
            'meta_query' => array(
                'relation' => 'OR',
                array(
                   'key'=>'my_sort',
                   'compare' => 'EXISTS'         
                ),
                array(
                    'key'=>'my_sort',
                    'compare' => 'NOT EXISTS'         
                )
            ),
        )
      );

I do not get proper result. See the image which shows the result.

Here:

Record1 : does not have "my_sort" meta.

Record2 : does have "my_sort" and it's value is 2.

Record3 : does not have "my_sort" meta.

Record4 : does have "my_sort" meta and it's value is 4.

The result in this case should be like this:

Record2 then Record4 then Record1 then Record4 but clearly this is not the case.

What is wrong?

введите описание изображения здесь

1 Ответ

1 голос
/ 07 августа 2020

Если вы хотите отсортировать по мета-ключу, который может не иметь никаких значений, вам нужно использовать meta_query, который объединяет результаты поиска сообщений с ключом и поиском сообщений без ключа.

Это не тестировалось, но основной лог c есть:

$catPost = get_posts( 
    array (
        'category' => get_cat_ID($categories[0]->name), 
        'numberposts' => 100, 
        'orderby' => 'meta_value_num', 
        'meta_type' => 'NUMERIC',
        'order' => 'ASC', 
        'meta_query' => array(
            'relation' => 'OR',
            array(
               'key'=>'my_sort',
               'compare' => 'EXISTS'         
            ),
            array(
                'key'=>'my_sort',
                'compare' => 'NOT EXISTS'         
            )
        ),
    )
);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...