0

I am working on a wordpress code as shown below in which the modified post displays at the top.

$temp_args = [
    'post_type' => array('current-channel', 'post', 'current-episodes'),
    'post_status' => 'publish',
    'orderby' => array(
        'feat_yes' => 'ASC',
        'post_type' => 'ASC',
        'modified' => 'DESC',
        'date' => 'DESC'),
    'posts_per_page' => $data->{"no_articles_" . ICL_LANGUAGE_CODE},
    'tax_query' => [
        [
            'taxonomy' => 'category',
            'field' => 'term_id',
            'terms' => $cat_today,
        ],
    ],

];
$q = new WP_Query($temp_args);

At this moment, order by is done in the following way:

'orderby' => array(
    'feat_yes' => 'ASC',
    'post_type' => 'ASC',
    'modified' => 'DESC',
    'date' => 'DESC'),  

Problem Statement:

I am wondering what changes I should make in the code above so that sticky posts are displayed at the top before the regular post.

The order should be in a way that sticky post should be always at the top and then all the regular post

1 Answer 1

0

I think you may need to add the feat_yes as a meta_key

$temp_args = [
    'post_type' => array('current-channel', 'post', 'current-episodes'),
    'post_status' => 'publish',
    'meta_key' => 'feat_yes',
    'orderby' => array(
        'meta_value_num' => 'ASC', /* or 'meta_value' */
        'post_type' => 'ASC',
        'modified' => 'DESC',
        'date' => 'DESC'),
    'posts_per_page' => $data->{"no_articles_" . ICL_LANGUAGE_CODE},
    'tax_query' => [
        [
            'taxonomy' => 'category',
            'field' => 'term_id',
            'terms' => $cat_today,
        ],
    ],

];
$q = new WP_Query($temp_args);

Here's a few relevant links:

4
  • Adding feat_yes as a meta_key would keep the sticky posts always at the top ? Commented Apr 2, 2019 at 2:57
  • 1
    Maybe, given that feat_yes isn't a core WordPress variable of any kind that I know of, I'm guessing it's post-meta, and a boolean, in which case yes, ordering by it would give priority to those posts with the value.
    – admcfajn
    Commented Apr 2, 2019 at 2:59
  • I added your code in my codebase. Now its not displaying any post, not a single one. Let me know if you need more information. Commented Apr 2, 2019 at 3:03
  • I don't have enough information about the problem to offer you a better solution. Good luck with it though!
    – admcfajn
    Commented Apr 2, 2019 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.