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 ? "⇦ {$previous_link}" : "" ).
"</span>".
"<span id=\"next-link\">".
( $next_link ? "{$next_link} ⇨" : "" ).
"</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.
UPDATE 2
Sadly, I'm not able to make it working after making the commented adaptations via js (additionally pass the number of the page of the pagination to query). Like when I do this query via AJAX:
$args = array(
'post_type' => 'my_post_type',
'tax_query' => $tax_query_array,
'orderby' => 'meta_value_num',
'meta_key' => 'my_metas_key',
'order' => 'ASC',
'posts_per_page' => 7,
'paged' => 2
);
I still get one of the posts already shown on page 1 (a random one).
Same if I add $args['offset'] = 7
and / or $args['page'] = 2
, so I'm confused; what am I still not properly understanding here??
pre_get_posts
to change its parameters? Pagination functions in WordPress all work off of the main query by default. Also if the main query has 4 pages but your query has 5, then the 5th page may not work as you expected because there is not 5th page on the main query. You might find it 404's instead. Also what file is this in? Is it a page template? Are you trying to build a CPT archive? What is it trying to implement?main
and not-main
, seemingly), but thought, the docs say you should use theWP_Query
class for page templates, so I'll most likely also use it in ajax calls..?the_post
orhave_posts
etc refer to, much like there's a current post. If your query is inside an AJAX callback that's super important information, AJAX requests do not have pagination, you have to provide it. It looks like you have not provided the full code, you'll need to edit your question to include this. Don't try to be helpful and hide things you think aren't relevant, that's probably were the error was