How navigation works in custom loop within shortcode?

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:

Read More
$the_query = my_custom_query();
if($the_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>';

  endwhile;

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

  else : $return_string =  'no result';  

 endif;
 }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(
        'post_type'=>any,
        'posts_per_page'=> 1,
        'max_num_pages'=> 10,
        'paged'=>$paged,
        '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){
if ($the_query->have_posts()) :
  while ($the_query->have_posts()) : $the_query->the_post();
    echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';

  endwhile;

  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';  

 endif;
 }else{ echo 'ordinary page';}

Related posts

Leave a Reply

1 comment

  1. 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.