Site icon Hip-Hop Website Design and Development

Pages of Pagination repeat already shown posts

I’m querying posts using the following wp query:

function custom_retrieve_posts_function() {

$paged = get_query_var( "paged" ) ? get_query_var( "paged" ) : 1;
$args = array(
        'post_type' => 'my_post_type',
        'posts_per_page' => 7,
        'paged' => $paged
);
$query = new WP_Query( $args );
$output = "";
if ( $query->have_posts() ) {

  while ( $query->have_posts() ) {

    // Iterate next post of query result
    $query->the_post();

    $output .= get_the_content();

  }

  // Only display arrows if links have been obtained, so do it in this way
  $previous_link = get_previous_posts_link(
    esc_html__( 'Zurück', 'custom-text-domain' )
  );
  
  $next_link = get_next_posts_link(
    esc_html__( 'Weiter', 'custom-text-domain' ),
    $query->max_num_pages
  );

  $nav_links = "<div id="navigation-links">".

    "<span id="previous-link">".
    ( $previous_link ? "&#8678 {$previous_link}" : "" ).
    "</span>".

    "<span id="next-link">".
    ( $next_link ? "{$next_link} &#8680" : "" ).
    "</span>".

  "</div>";

  $output .= $nav_links;

  // Reset the wp_query loop
  wp_reset_postdata();
      
 } else {

   $output = "<p class="no-results">Oops!</p>";

}

echo $output;

}

Problem is that certain posts of page n are seemingly randomly repeated on page(s) m, with m > n.

The amount / limit of posts to be shown in feeds and blogs is 7, I’ve set that in the wp admin options. Still, same problem persists.

Any idea why this is happening?

UPDATE

The function custom_retrieve_posts_function() (defined in a namespace within a class etc., but all of this is omitted here for simplicity) is called within a PHP script bound to the AJAX hook responsible for executing a custom filtered query via ajax.

So the script is called like this:

Main Plugin File holds this:

add_action(
  'wp_ajax_myplugin_search_custom',
  function() {
    require MYPLUGIN_AJAX_DIR.'/custom_search.php';
    wp_die();
  }
);

add_action(
  'wp_ajax_nopriv_myplugin_search_custom',
  function() {
    require MYPLUGIN_AJAX_DIR.'/custom_search.php';
    wp_die();
  }
);

With the content of AJAX_DIR.'/custom_search.php' being (again simplified, all naming conflicts are 100% avoided, so all classnames / namespaces are again omitted):

check_ajax_referer( 'ajax-nonce-content' );
require MYPLUGIN.'/custom_search_function.php';
custom_retrieve_posts_function();

I then also have a page template defined in my themes/mycustomtheme/templates directory, and within that page template, I simply call

require MYPLUGIN.'/custom_search_function.php';
custom_retrieve_posts_function();

Note that all of this works perfectly, expect from the problem that already displayed posts are re-displayed on subsequently paginated pages.