最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览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

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
发布评论

评论列表(0)

  1. 暂无评论