calendar – faking $wp_query->post_founds – WordPress Development Stack Exchange


i’m trying to make a calendar without plugin

Most part of the job is to display multiple times a post (ie : a event like a concert can have 3 dates )
I’ve firstly try to create a post by date, so duplicating post for differents dates, but it ending with tons of posts, and managing them with a front-end page for user as one unic event (create /modify / delete) is time-consuming and donesn’t seems a well practice.

So as I can’t make a regular loop to display a same (post) multiples times in a wp_query, I’ve seen a good idea : having a taxonomy call “event_date” and adding each dates of the post in.

The concept of the code is :

  • Find all no empty taxonomy terms (get_term)
  • make a foreach on them to … (foreach)
  • … loop each dates to find post related with (wp_query)
    and you’ve got a list like
• 2023-05-18
- name of an event A
- name of an event B

• 2023-05-22
- name of an event C

• 2023-05-22
- name of an event A
- name of an event B

• 2023-05-23
- name of an event C

• 2023-05-24
- name of an event c
- name of an event B

• 2023-05-30
- name of an event C

• 2023-06-15
- name of an event C

It looks working like a charm, until I’m try the pagination. Making the pagination cause me 404 link on certains last pagination-links. I explained the fact that I can have more dates than event pages. For example the main loop is waiting for 5 posts (i ‘ve got 5 posts for now in my event CPT) and the posts_per_page on WP dashboard is set to 2. So WP will need 5/2 = 2,5 -> 3 pages to display them.

But if I’ve got 7 dates in my taxonomy, I’m waiting for 7/2 = 3,5 -> 4 pages of pagination.

And that’s why my “4” pagination link fall into a 404 page.

THIS is approximately understood.
So my first thought was pre_get_post but my main loop is a foreach, so no impact on the main query.

So i’ve try to brutaly change in pre_get_post something like :

$wp_query->max_num_pages = 4;
$wp_query->post_founds = 7;

… but there no effect (naive!)

SO! my BIG question :

  • how can I fake all this ?
  • making wordppress believe to aa fake max_num_pages and post_count / post_count values

Thank’s all

nico 2 weeks 2023-03-15T06:00:29-05:00 0 Answers 0 views 0

Leave an answer