0

The description

Hi, I am using a custom post type and a loop through the values ​​I show.

I'm using a term ($ term) that comes from a previous page in the loop to only show related items.

Just for a test run I configured in the administration of wordpress in the reading section, show only two post.

I'm also using a custom post type associated taxonomy as follows:

<?php 
  // Argumentos del loop
  $argumentos = array (
    'post_type' => 'placas',
    'paged' => $paged,
    'tax_query' => array(
      array(
        'taxonomy' => 'firmas',
        'field' => 'slug',
        'terms' => $term
      )
    )
  );

  $query_productos= new WP_Query($argumentos);
?>

The pagination made ​​this way works well

<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>

The Problem

But I need to display 4 post, so try this.

This shows me 4 post related to the term

 <?php 
  // Argumentos del loop
  $argumentos = array (
    'post_type' => 'placas',
    'posts_per_page' => 4,
    'paged' => $paged,
    'tax_query' => array(
      array(
        'taxonomy' => 'firmas',
        'field' => 'slug',
        'terms' => $term
      )
    )
  );

  $query_productos= new WP_Query($argumentos);
 ?>

The problem is that the links next returns me an extra one, without content. Only have 6 post published and I should only have 2 pages for paging, but actually get 3 and the last third is empty.

How I can fix it so that only the necessary pages have not an extra that is empty?

1 Answer 1

1

The issue is that you are running a custom query ($query_productos), but your next_posts_link is using information from the main query ($wp_query) to determine whether or not to show a link to the next page.

See the example on next_posts_link for use with WP_Query, by passing max_num_pages from your custom query -

<?php next_posts_link( 'Older posts', $query_productos->max_num_pages ); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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