Ask your WordPress questions! Pay money and get answers fast! (more info)

get_adjacent_post order by metakey - event's date WordPress

Here you can see my problem:

http://www.manlioma.it/pigneto/civil-civic-molotoy/

this is an event, and I want to have next and previous event, like next and previous post.

every post has custom field called "data", that is the day of event.

I want to go, with next and previous links, to next and previous day, only in one category.

now I use this one, but it works with the date of publishing not with my custom field.

<?php if (get_adjacent_post(true, '4', true)): // if there are older posts ?>
<li class="prev clearfix"><span><< DAY BEFORE </span></br> <?php previous_post_link('%link'); ?></li>
<?php endif; ?>
<?php if (get_adjacent_post(true, '4', true)): // if there are newer posts ?>
<li class="next clearfix"><span>DAY AFTER >></span></br> <?php next_post_link('%link'); ?></li>
<?php endif; ?>


anyone helps me ?

Answers (3)

2013-06-22

Giri answers:

You should use order by custom field meta key in the query to make it work.

I guess you should try this plugin

http://wordpress.org/plugins/ambrosite-nextprevious-post-link-plus/

with this answer

http://wordpress.stackexchange.com/a/101768/5074

2013-06-22

Arnav Joy answers:

so you want to show next post as based on the date given on custom field? within same category?


Manlio Ma comments:

yes, and if the next day we don't have a post, I want to see the day after

2013-06-22

Dbranes answers:

One could play around with these filters:

add_filter( 'get_next_post_where', 'get_next_post_where_callback' );
add_filter( 'get_previous_post_where', 'get_previous_post_where_callback' );
add_filter( 'get_next_post_sort', 'get_next_post_sort_callback' );
add_filter( 'get_previous_post_sort', 'get_previous_post_sort_callback' );
add_filter( 'get_next_post_join', 'get_next_post_join_callback' );
add_filter( 'get_previous_post_join', 'get_previous_post_join_callback' );


where

function get_previous_post_join_callback( $join ){
global $wpdb;
$join .= " INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id ";
return $join;
}
function get_next_post_join_callback( $join ){
global $wpdb;
$join .= " INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id ";
return $join;
}
function get_previous_post_where_callback( $where ){
global $post;
$where = preg_replace( "/p.post_date([^AND].*)AND/i","", $where );
$where .= " AND CAST( pm.meta_value AS DATE ) < '". get_post_meta($post->ID, 'data', true)."' ";
$where .= " AND pm.meta_key='data' ";
return $where;
}
function get_next_post_where_callback( $where ){
global $post;
$where = preg_replace( "/p.post_date([^AND].*)AND/i","", $where );
$where .= " AND CAST( pm.meta_value AS DATE ) > '". get_post_meta($post->ID, 'data', true)."' ";
$where .= " AND pm.meta_key='data' ";
return $where;
}
function get_previous_post_sort_callback( $sort ){
$sort = " ORDER BY CAST(pm.meta_value AS DATE) DESC LIMIT 0,1 ";
return $sort;
}
function get_next_post_sort_callback( $sort ){
$sort = " ORDER BY CAST(pm.meta_value AS DATE) ASC LIMIT 0,1 ";
return $sort;
}