Sort custom post with custom field not working

I am working with wordpress where i have a sort listing. There is a custom field in my post called “value_event” which date format yyyy/mm/dd. Now i want to sort all posts by value_event. with this query_post command:

This is my code in template for custom post taxonomy-event-date.php

   <?php
   $args = array(
  'post_status'       => 'publish',
  'posts_per_page'    => 6,
  'paged'             => $paged,
  'meta_key'          => 'value_event',
  'orderby'           => 'meta_value_num',
  'order'             => 'ASC'
  );
  $query = new WP_Query( $args );
  if ( $query->have_posts() ) {
  while ( $query->have_posts() ) {
  $query->the_post();?>
  <?php the_title(); ?>
  <?php                          
  }
  }
  wp_reset_query();  // Restore global post data stomped by the_post().
  ?> 

Functions

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

Read more here: Sort custom post with custom field not working

Leave a Reply

Your email address will not be published. Required fields are marked *