Support » Themes and Templates » Paging inside "single" template using custom post type & custom query

  • On the website for my running team I have a list of all active team members. I created a custom post type called tpb_teammember which has a few custom fields, amongst which one called teammember_active. In the listing template I can easily generate the correct list using a custom query that I pass to the WP_Query object, so that the list only contains the team members which have teammember_active set to “yes”.

    But once inside a single team member page, the paging to the previous and next teammember page no longer respects this custom query, since the context is lost. So I have been trying several different approaches to pass once again the same custom query to the WP_Query object so that when I use previous_post_link() and next_post_link(), they only display links to the previous/next active team member…but so far I can’t seem to get this working.

    Here is the query that I use on the team members listing page:

    $args = array(
    	'post_type' 	=> 'tpb_teammember',
    	'orderby'	=> 'title',
    	'order'		=> 'ASC',
    	'posts_per_page'=> -1,
    	'meta_query' 	=> array(
    					'key' 		=> 'teammember_active',
    					'compare'	=> '=',
    					'value' 	=> 'yes'

    How can I apply this query to the single template so that the pagination links only display links to active team member pages?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Never mind, waiting for an answer here took too long, so I decided to build a custom paging link function from scratch. It’s quick and dirty and might not work for all situations, but it works for mine. So here it is.

    A rundown of the function’s arguments:

    $format and $link: Work in the same way as the $format and $link arguments in the previous_post_link() and next_post_link() function.

    $offset: can be a positive or negative integer, relative to the current post held by the global $post object. So, a value of 1 would give the next post and -1 would give previous one. But any other value will work. If the offset exceeds the range of the set of posts, nothing is returned.

    $post_type: self-explanatory. Function is designed to only to query posts of one post type.

    $order_by: the name of the field inside your post by which to order your set of posts.

    $custom_query_array: An array of arrays of key-value pairs that contains the parameters for the custom query. Same format as when composing a custom meta_query to be passed to the WP_Query object.


    $custom_query = array(
    		'key' 		=> 'teammember_active',
    		'compare' 	=> '=',
    		'value' 	=> 'yes'
    <div id="nav-above" class="navigation">
    	<div class="nav-previous"><?php echo get_offset_post_link('%link', '<span class="meta-nav">' . _x( '←', 'Previous post link', 'powerpuff' ) . '</span> %title', -1, 'tpb_teammember', 'title', $custom_query); ?></div>
    	<div class="nav-next"><?php echo get_offset_post_link('%link', '%title <span class="meta-nav">' . _x( '→', 'Next post link', 'powerpuff' ) . '</span>', 1, 'tpb_teammember', 'title', $custom_query); ?></div>
    </div><!-- #nav-above -->

    Hi indorock,

    Thanks to post your solution. That what I research to a sorting problem…

    Do you have some other news on a wordpress native method ?

    If I don’t find other solution I’ll take yours. 😉

    I’m going to try this plugin :

    If it can help someone else…

    The ambrosite-nextprevious-post-link-plus plugin run perfectly on WP 3.4.1 !

    Just one thing : the indorock allows cascading multiple sorts / the plugin is more easy to use but don’t allow more than 2 multiple sorting in cascading.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Paging inside "single" template using custom post type & custom query’ is closed to new replies.