$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Is it possible to sort the post based on a custom field?|Programmer puzzle solving
最新消息: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)

Is it possible to sort the post based on a custom field?

matteradmin9PV0评论

I have a custom field called "value_date" where add a date e.g yyyy/mm/dd

The idea is sort the upcoming dates first in the loop, is possible?

I used a template for custom post taxonomy-event-date.php and this is code in the function, It doesn't work.

Functions.php

add_action('pre_get_posts','search_filter');
function search_filter($query){
    if ( !is_admin() && $query->
is_tax( 'event', 'date' ) ) {
        $today = date( 'Y-m-d' );
        $query->set('post_status', 'publish');
        $query->set('meta_value', $today);
        $query->set('meta_key', 'value_date');
        $query->set('orderby', 'meta_value_num');
        $query->set('order', 'ASC');
    }
}

I have a custom field called "value_date" where add a date e.g yyyy/mm/dd

The idea is sort the upcoming dates first in the loop, is possible?

I used a template for custom post taxonomy-event-date.php and this is code in the function, It doesn't work.

Functions.php

add_action('pre_get_posts','search_filter');
function search_filter($query){
    if ( !is_admin() && $query->
is_tax( 'event', 'date' ) ) {
        $today = date( 'Y-m-d' );
        $query->set('post_status', 'publish');
        $query->set('meta_value', $today);
        $query->set('meta_key', 'value_date');
        $query->set('orderby', 'meta_value_num');
        $query->set('order', 'ASC');
    }
}
Share Improve this question edited May 16, 2017 at 17:52 Max asked Apr 19, 2017 at 16:22 MaxMax 536 bronze badges 1
  • 1 It's possible, look around for pre_get_posts questions, however- your date format is incorrect and will need to be changed to yyyy/mm/dd to sort correctly. – Milo Commented Apr 19, 2017 at 17:09
Add a comment  | 

1 Answer 1

Reset to default 1

Yes, You can call action "pre_get_posts".

add_action('pre_get_posts','search_filter');

You can add parameter :

$args = array(
  'post_status'       => 'publish',
  'posts_per_page'    => 6,
  'paged'             => $paged,
  'meta_key'          => 'event_date',
  'orderby'           => 'meta_value_num',
  'order'             => 'ASC'
);

You can use above parameter e.g. :

function search_filter($query){
    if ( !is_admin() && $query->is_main_query() ) {
        $query->set('post_status', 'publish');
        $query->set('meta_key', 'event_date');
        $query->set('orderby', 'meta_value_num');
        $query->set('order', 'ASC');
    }
}
Post a comment

comment list (0)

  1. No comments so far