最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - How to display sticky post always at the top (before regular post) in wordpress?

matteradmin5PV0评论

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

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

Share Improve this question edited Apr 2, 2019 at 2:43 user5447339 asked Apr 2, 2019 at 2:18 user5447339user5447339 819 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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:

  • https://codex.wordpress/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
  • WP_Query - Order results by meta value
  • Custom query with orderby meta_value of custom field
Post a comment

comment list (0)

  1. No comments so far