WIP: Post Archives by Year don't completly work with Ajax Pagination?

I’m trying to setup Archives for my Posts to display them by year. My current Code works kind of:
I can paginate back and forth to all pages but the inital page. once i want to paginate back to page 1 it doesn’t work (no posts are being fetched). I think it’s a bug, since useing the same template for my categories works just fine.

In the pictures you see me useing a section template within a archive template. The query is inside the section template, and the archives are displayed correctly and work in all cases but not for navigating back to the first pagination (with ajax)



best regards
Suat

Hi @UserfreundSuat,

can you double-check if this is the only “main query” on that page?

Also, can you share a link to the website, so I can see the issue - if possible, also record a short video of your setup and how to replicate it.

Thank you,
Matej

Hi there,

Here is a short video explaining the potential bug:
https://jam.dev/c/8278f9d9-d1d3-47d1-84cf-9dd835f86aa8

The site is not yet launched, so there is a maintenance mode active, which can be bypassed with this url:
https://staging.sk-koenigsbruegge.de/?bypass=uf

And below is the code for the custom query… which should not be responsible but just in case.

Best regards
Suat

<?php

namespace Userfreunde;

if (! defined('ABSPATH')) exit; // Exit if accessed directly

class Misc

	public function __construct()
	{
		add_filter('bricks/setup/control_options', [$this, 'add_query_control_post_years']);
		add_filter('bricks/query/run', [$this, 'run_query_post_years'], 10, 2);
		add_filter('bricks/query/loop_object', [$this, 'setup_loop_object_post_years'], 10, 3);

		add_filter('bricks/frontend/render_element', [$this, 'modify_world_svg_group_attributes'], 10, 2);
	}

	/**
	 * MARK: Add Query Control
	 * @param array $control_options
	 * @return array
	 */
	public function add_query_control_post_years($control_options)
	{
		$control_options['queryTypes']['post_years'] = esc_html__('Post Years');

		return $control_options;
	}

	/**
	 * MARK: Run Query
	 * @param array $results
	 * @param object $query_obj Bricks\Query
	 * @return array
	 */
	public function run_query_post_years($results, $query_obj)
	{
		if ($query_obj->object_type !== 'post_years') {
			return $results;
		}

		global $wpdb;

		$post_type = 'post';

		$years = $wpdb->get_col($wpdb->prepare("
			SELECT DISTINCT YEAR(post_date) as year 
			FROM {$wpdb->posts} 
			WHERE post_status = 'publish' 
			AND post_type = %s 
			ORDER BY year DESC
		", $post_type));

		if (empty($years)) return $results;

		return $years;
	}

	/**
	 * MARK: Setup Loop Object
	 * @param array $loop_object
	 * @param string $loop_key
	 * @param object $query_obj Bricks\Query
	 * @return array
	 */
	public function setup_loop_object_post_years($loop_object, $loop_key, $query_obj)
	{
		if ($query_obj->object_type !== 'post_years') {
			return $loop_object;
		}

		if (empty($loop_object)) return $loop_object;

		$data = [
			'year' => $loop_object,
			'url' => get_year_link($loop_object),
		];

		return $data;
	}
...

Hi @UserfreundSuat,

thank you for your video and website url - I see what is happening. If you are on a /2025 page, and you click on the first page, the query will wrongly assume that the 2025 is a page you want to query… and that’s why there are not results.

I’ve created an internal bug report, but as a workaround, you can disable AJAX pagination, then it should work.

Thank you,
Matej

1 Like