WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using a shortcode to display a custom query. The shortcode is use in page. Everything is fine, only the navigation that I can't get it working correctly.

Here is the function that I use to display the query calling by the shortcode:

$the_query = my_custom_query();
if ($the_query->have_posts()) :
  while ($the_query->have_posts()) : $the_query->the_post();
    echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';


  next_posts_link(); previous_posts_link(); //here I can't get it right.

  else : $return_string =  'no result';  

 }else{ echo 'ordinary page';}

The previous_posts_link() and next_posts_link() not showing up. Is it because the shortcode been inside the page main loop?

Update this is the function to get the custom query

function my_custom_query(){
         $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;    
         $args = array(
        'posts_per_page'=> 1,
        'max_num_pages'=> 10,
        'meta_query' => array('relation'=>'OR',array(
                    'taxonomy' => 'tax1',
                'field' => 'slug',
                'terms' => 'tax1term'
        'tax_query' => array('relation'=>'OR',array(
                'key' => 'metakey',
                'value' => array(5,30),
                'compare' => 'compare'


       $custom_query = new WP_Query( $args);
        return $custom_query;


UPDATE 2 Now I can get the next_posts_link(); previous_posts_link(); to display. But the problem is, it linked to a wrong page. The query results remain same posts on every page (regardless if it is Next or Previous page).

$the_query = my_custom_query();
if ($the_query->have_posts()) :
  while ($the_query->have_posts()) : $the_query->the_post();
    echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';


  previous_posts_link('Previous',$the_query->max_num_pages); next_posts_link('Next',$the_query->max_num_pages);  //here I added $the_query->max_num_pages,and they shows up. 
  else : $return_string =  'no result';  

 }else{ echo 'ordinary page';}
share|improve this question
Concerning the updated query code: Reread Milo's answer & comments below. Again, $max_num_pages is the total number of pages, the result of $found_posts / $posts_per_page. If you set posts_per_page to 1 and the maximum number of pages also to 1 as well --> how do you expect to get more than one post to show? – Johannes Pille Oct 22 '12 at 18:13
I am sorry, it was a typo, it should be 10,now updated. – dev-jim Oct 22 '12 at 18:47

if you look at previous_posts_link() and next_posts_link() in source, you'll see why they don't work, they use the global $paged and $wp_query vars to format the links and determine whether or not they appear. you'll have to roll your own pagination using the paged and max_num_pages vars in your custom query.

share|improve this answer
I already added the paged in my custom query. The max_num_pages somehow is not working. It does not limit the post, so I use posts_per_page instead. But the next_posts_link() and previous_posts_link() still doesn't works. – dev-jim Oct 21 '12 at 19:33
max_num_pages is how next_posts_link determines whether or not there are more pages to display. if you don't pass that value, the link will never show because pages only have a single page. again, look at the functions in source to see how they work. – Milo Oct 22 '12 at 15:29
yes, i also added max_num_pages in the query, but still not working. – dev-jim Oct 22 '12 at 15:41
I think you misunderstand the purpose of max_num_pages, it is not something you set in a query. add your query code and what you've actually tried to your question or i'm unable to help you further. – Milo Oct 22 '12 at 15:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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