1

I'm using the following code to paginate between custom post types and it doesn't seem to be using the meta_key or meta_value_num and I can't figure out why. The CPT is set-up to have hierarchal => true and the Capability type is a page.

This is the code I'm using for paging (which doesn't seem to make a difference--I tried using orderby=menu_order and that didn't work either).

<?php if
(get_adjacent_post(false, '', true))
{  echo previous_post_link('%link' , '<i class="fa fa-chevron-left"></i>');  }
else
{ echo previous_post_link('%link' , '<i class="fa fa-chevron-right"></i>');
$newest_post = get_posts('post_type=employee-bios&posts_per_page=1&order=DESC&meta_key=display-order&orderby=meta_value_num');  $newest_post_id = $newest_post[0]->ID;
echo '<a href="'.get_permalink($newest_post_id).'">
</a>'; } 
?>

<?php if
(get_adjacent_post(false, '', false))
    {  echo next_post_link('%link' , '<i class="fa fa-chevron-right"></i>'); }
else
    { echo ''; $eldest_post = get_posts('post_type=employee-bios&posts_per_page=1&order=ASC&meta_key=display-order&orderby=meta_value_num');  $eldest_post_id = $eldest_post[0]->ID;
echo '<a href="'.get_permalink($eldest_post_id).'" title="link to '.get_the_title($eldest_post_id).'"><i class="fa fa-chevron-right"></i></a>'; } ; 
?>

And this is my WP_Query (which doesn't seem to matter in the scheme of things either--I'd started off just using while have posts and when that didn't work, I tried the WP_Query)

<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'post_type' => 'employee-bios',
    'posts_per_page' => 1,
    'orderby' => meta_value_num,
    'meta_key' => 'display-order',
    'order' => 'ASC',
    'paged' => $paged
);
$my_query = new WP_Query( $args );
?>

My client wants to be able to select the order of the employee bios on a landing page and they should paginate in the order they've been placed on the landing page (which is why I tried using both menu_order and a custom field meta value for sorting).

I've also tried changing the Capability type from page to post and that doesn't seem to make a difference either.

Any help would be much appreciated. Thanks!

1

I just found the answer to this at another Stack... as is always the way when I post a question, it seems. No matter what one does, in regards to pagination in this situation, WP will always go by post date first. I needed to add a function that would use menu_order and found the answer here.

function wpse73190_gist_adjacent_post_sort( $sql ) {
    $pattern = '/post_date/';
    $replacement = 'menu_order';

    return preg_replace( $pattern, $replacement, $sql );
}

add_filter( 'get_next_post_sort', 'wpse73190_gist_adjacent_post_sort' );
add_filter( 'get_previous_post_sort', 'wpse73190_gist_adjacent_post_sort' );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.