Is it possible to sort the post based on a custom field?
I have a custom field called "value_date" where I enter 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
<!-- // The Query // -->
<?php
if( have_posts() ) {
$alt_post = 'alt-post';
while( have_posts() ) {
the_post();
$alt_post = $alt_post == 'alt-post' ? '' : 'alt-post';
?>
<div class="box-page-shows-block">
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'value_date', true);
wp_reset_query();
?>
</div>
<h2><?php the_title(); ?></h2>
<?php
}
}
?>
EDIT Not working only displayed one post Loop in template for custom post taxonomy-event-date.php
<?php
$args = array(
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
'meta_key' => 'value_date',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();?>
<h2><?php the_title(); ?></h2>
<?php
}
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Functions.php
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');
}
}