NO BUG: Duplicate Posts with Infinite Scroll and Menu Order

Hi,

I’m working on a project and believe I may have found a bug in the query loop with infinite scroll.

When I set the order by “Menu order” and use infinite scroll, the loop loads random duplicate posts after an AJAX load.


Hi Caine,
Thanks so much for your report, and welcome to the forum!

Please have a look at this article (german source, but you can probably translate in your browser):

Long story short:
If the order value is the same for several posts (probably 0), the database is not able to determine a reliably identical sequence. Each time the posts are queried, a random sequence of posts with the same value is created. This means that the same content can also appear on pages one and two of your loop.

You can fix this by adding this filter, which adds a second orderby value (the title), to a code snippet plugin or your child theme’s functions.php:

add_filter('bricks/posts/query_vars', function(array $query_vars, array $settings, string $element_id) {
	if (!empty($query_vars['orderby']) && $query_vars['orderby'] === 'menu_order') {
        $query_vars['orderby'] = 'menu_order title';
    }
    return $query_vars;
}, 10, 3);

Best regards,
timmse

2 Likes

I have the same issue with infinite scrolling and order by numeric meta value. The order value is the same on several posts and infinite scroll will therefore create random duplicates. I tried this snippet

add_filter('bricks/posts/query_vars', function(array $query_vars, array $settings, string $element_id) {
	if (!empty($query_vars['orderby']) && $query_vars['orderby'] === 'meta_value_num') {
        $query_vars['orderby'] = 'meta_value_num ID';
    }
    return $query_vars;
}, 10, 3);

With the post ID being the secondary order value. This seems to be working fine. But when I sort the listing with JetSmartFilter, sorting by the numeric meta value with it, ascending or descending it will again create duplicates.

I could just use JetEngines Query Builder, because there you can add multiple sorting values and it will work with JetSmartFilters, but it seems that JetEngine doesn’t have an infinite scroll functionality. At least not that I could find.

@timmse thanks for linking the article. Actually added translation couple of weeks ago :slight_smile:

Nonetheless, @alexplex it might be the issue a missing “order” variable. Could you try the following snippet:

add_filter('bricks/posts/query_vars', function(array $query_vars, array $settings, string $element_id) {
	if (!empty($query_vars['orderby']) && $query_vars['orderby'] === 'meta_value_num') {
		$query_vars['orderby'] = array(
			'meta_value_num' => $query_vars['order'] ?? "ASC",
			'ID' => $query_vars['order'] ?? "ASC"
		);
	}
	return $query_vars;
}, 10, 3);

The only difference is it either uses the preset order variable or falls back to “ASC”

Thanks for the suggestion @JUVO_Justin but I actually realized that infinite scroll was a bad idea in my case. Using a “Show more” button was better. My website is since published :wink: