Build a Page which shows the Oldest Post with specific Custom Field Value

I have searched all over for a solution to this problem, so forgive me if I was searching for the wrong thing..

I have a Page “Review” (slug “review”).
I have a number of Custom Post Types with a Custom Field called asset_status.
I want to be able to go to the URL for the Review Page, and be shown the oldest Custom Post with waiting as the value for the asset_status Custom Field.
Ideally, I would also like to be able to have “Prev”/”Next” Buttons allow the user to go to the 2nd/3rd/4th Oldest Post with that field value.

I have tried overriding the Page Query through the pre_get_posts and parse_request hooks. I have had some success with parse_request, but have then had trouble getting the site to use the single.php template, even though I try and change it through the template_include hook (although I wonder whether my test of is_page( ‘review’ ) at that stage fails because I overrode the query earlier.

add_filter( ‘parse_request’, function( $query ) {

// triggered also in admin pages
if ( is_admin() )
return $query;

// you should check also for page slug, because when pretty permalink are active
// WordPress use ‘pagename’ query vars, not ‘page_id’
$id = isset($query->query_vars[‘page_id’]) && (int) $query->query_vars[‘page_id’] === 230;
// remember to replace “slug” with real page slug
$name = isset($query->query_vars[‘pagename’]) && $query->query_vars[‘pagename’] === ‘review’;

if ( ( $id || $name ) && !isset( $query->query_vars[‘error’] ) ) {
$query->query_vars = array(
‘post_type’ => ‘asset’ ,
‘meta_key’ => ‘asset_status’ ,
‘meta_value’ => ‘waiting’ ,
‘post_status’ => ‘publish’ ,
‘posts_per_archive_page’ => 1 ,
‘orderby’ => ‘date’ ,
‘order’ => ‘ASC’ ,
‘paged’ => ( get_query_var(‘paged’) ? get_query_var(‘paged’) : 1 )
);
}

return $query;
});

add_filter( ‘template_include’, function( $template ){

if ( is_page( ‘review’ ) ) {
die(‘THIS IS THE REVIEW PAGE’);
$new_template = locate_template( array( ‘single.php’ ) );
if ( ” != $new_template )
return $new_template;
}

return $template;

}, 99 );

Any advice, direction or suggestion would be appreciated.

Read more here:: Build a Page which shows the Oldest Post with specific Custom Field Value

Leave a Reply

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