0

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

#UPDATE This code is basically in the index.php but not in the main loop. This code works, but as it's not in the loop I need the wp_query updated in order to expects the same number of page type: "/page/x"


        $custom_cats = 'events_dates';

        $big = 999999999;

        $page = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

        $per_page = get_option( 'posts_per_page' );

        $offset = ( $page - 1 ) * $per_page;

        $term_args = array(
            'number'        => $per_page,
            'offset'        => $offset,
            'hide_empty'    => true,
        );

        $terms = get_terms( $custom_cats, $term_args );

        if ($terms) {

            foreach ( $terms as $term ) {
                $term_link = get_term_link( $term );

                echo '<li>';

                wp_reset_query();

                $args = array('post_type' => 'events', 
                    'tax_query' => array(
                        array(
                            'taxonomy' => $custom_cats,
                            'field' => 'slug',
                            'terms' => $term->slug,
                        ),
                    ),
                );

                $loop = new WP_Query($args);

                if($loop->have_posts()) {

                    echo '<h3><a href="'.$term_link.'">'.$term->name . ' ( ' . $term->count . ' )</a></h3>';

                    echo "<ul>";

                    while($loop->have_posts()) : $loop->the_post();

                        echo "<li>";

                        echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br/>';

                        echo "</li>";

                    endwhile;

                    echo "</ul>";

                }

                echo '</li>';
            }   

        }

        bootstrap_pagination( $wp_query  );

Thank's all Nico.

9
  • I'm currently here : interesting function "wp_add_cache" barn2.com/blog/create-fake-wordpress-post-fly
    – nico
    Mar 15 at 11:10
  • I'm not sure if you're actually doing it correctly, but you can use the found_posts filter hook to modify the found_posts value.
    – Sally CJ
    Mar 15 at 12:22
  • So what the correct approch please ? I've followed : weusewp.com/tutorial/paginate-taxonomy-terms
    – nico
    Mar 15 at 13:29
  • Sorry, I don't really know about the proper approach for what you're trying to do. I was only responding to the way you were attempting to modify the found_posts value. However, if you can provide more details like where are you doing this (what page is it), what template you are using, the template code, sample posts, sample output, etc., then maybe I can help you further.
    – Sally CJ
    Mar 15 at 13:55
  • Ok I misundestood, the code is updated in the inital post. Thank's.
    – nico
    Mar 15 at 14:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.