1

I have created 9 different Custom Post Types via ACF and an assortment of custom field blocks for 8 of them. Those 8 are used to create different content, based on requirements for each post type. The 9th one (titled: "Resources"), I created simply to be an archive repository for ALL of the posts that have been built. Every post created has been assigned to categories created in the categories archive. The individual post archives are functioning just fine. The posts are showing, sorting as desired AND the pagination is working on each.

For the Resources archive, my requirements are to:

  1. Combine all the post types into 1 "Resources" archive (completed).
  2. Sort the posts on the "Resources" archive by their publish dates (completed).
  3. Be able to filter these posts based on their categories - using drop down selectors (not completed).

Issue #1:

  • My pagination is not working - it doesn't show on the page at all. The row is in the source code, but the functions are missing. This pagination works with my other archive pages, so, I'm not sure why this one is refusing to work.

Currently, there are 41 total posts. There will be lots more. By changing the "posts_per_page" to "41" or say a ridiculous number like "50" or "60", all of the posts show up, sorted by publish date, as expected.

However, when keeping the number at "20" per page, the pagination code does not display.

enter image description here

Issue #2:

  • I have no idea how to filter the combined posts by the categories they've been assigned.

My template code is below:

<div id="primary" class="content-area">
    <main id="main" class="site-main">
        <div class="container resources">
        <?php 
        $cptArgs = [  
            'post_type' => array( 
                'blogs',
                'case-study',
                'datasheet', 
                'event', 
                'in-the-news', 
                'press-release', 
                'video', 
                'webinar'
            ),
            'posts_per_page' => 20,
            'paged' => get_query_var( 'paged' ),  
            'post_status' => 'publish',
            'meta_query' => array(
                'relation' => 'OR',
                'cpt_date_field' => array(
                    'key' => 'CPT_Images_published-date',
                    'type' => 'DATE'
                ),
                'article_date_field' => array(
                    'key' => 'article_date',
                    'type' => 'DATE'
                ),
                'event_date_field' => array(
                    'key' => 'event_specific_data_event_date_start',
                    'type' => 'DATE'
                ),
                'case_study_date_field' => array(
                    'key' => 'CPT_cs_hero_section_CPT_Images_published-date',
                    'type' => 'DATE'
                )
            ),
            'orderby' => array(
                'cpt_date_field' => 'DESC',
                'case_study_date_field' => 'DESC',
                'article_date_field' => 'DESC',
                'event_date_field' => 'DESC',
            ),
        ];
        $cptQuery = new WP_Query($cptArgs);
        ?>
        <?php if ( $cptQuery ) { ?>
            <div class="row">
                <form id="filterCPT">
                    <div class="row">
                        <div class="col-3 col-md-3 col-sm-3">
                        </div>
                        <div class="col-3 col-md-3 col-sm-3">
                            <select name="marketFilter" id="marketFilter">
                                <option label="- Filter By Market -">
                                <option value="retail-agency">Retail Agency</option>
                                <option value="mga">MGA</option>
                                <option value="wholesaler">Wholesaler</option>
                                <option value="carrier">Carrier</option>
                            </select>
                        </div>
                        <div class="col-3 col-md-3 col-sm-3">
                            <select name="cptFilter" id="cptFilter">
                                <option label="- Filter By Content Type -">
                                <option value="blogs">Blogs</option>
                                <option value="case-studies">Case Studies</option>
                                <option value="datasheets">Datasheets</option>
                                <option value="events">Events</option>
                                <option value="in-the-news">In The News</option>
                                <option value="press-releases">Press Releases</option>
                                <option value="videos">Videos</option>
                                <option value="webinars">Webinars</option>
                            </select>
                        </div>
                        <div class="col-3 col-md-3 col-sm-3">
                        </div>
                    </div>
                </form>
                <script>
                    $('form#filterCPT select').on('change', function(){
                        $(this).closest('form').submit();
                    });
                </script>
            </div>
            <div class="row row-cols-1 row-cols-md-3 g-4 cardLayout">

                <?php while ( $cptQuery->have_posts() ) : $cptQuery->the_post(); 
                    setup_postdata( $cptQuery );
                    include( get_stylesheet_directory() . '/template-parts/vars-cpt-resources.php');
                    ?>
                    <div class="col-3 col-md-3 col-sm-3 notFeatured">
                        <div class="card h-100">
                            <span class="cardImgWrap" role="img" aria-label="<?php echo $thumbDesc; ?>">
                                <img class="card-img-top" src="<?php echo $thumbUrl; ?>" alt="<?php echo $thumbAlt; ?>" title="<?php echo $thumbTitle; ?>" />
                            </span>
                            <div class="card-body">
                                <h5 class="card-title"><?php echo $h1Copy; ?></h5>
                                <p class="card-text"><?php echo $excerpt; ?></s>
                            </div>
                            <a class="ctaButton clickTrackCTAButton blogs" href="<?php the_permalink();?>" title="<?php echo $h1Copy; ?>">
                                <?php echo $ctaButtonIcon; ?><?php echo $ctaButtonTitle; ?>
                            </a>
                        </div>
                    </div>
                <?php endwhile; ?>
            </div> <!-- row -->
            <div class="row pagination">
                <div class="col-12 col-md-12 col-sm-12 paginationRow">
                <?php
                    the_posts_pagination( array(
                        'mid_size'  => 3,
                        'prev_text' => _( 'Previous Page' ),
                        'next_text' => _( 'Next Page' ),
                    ) );
                ?>
                </div>
            </div>
            <?php wp_reset_postdata(); ?>
        <?php } else { ?>
            <?php echo esc_html__( 'No posts matching the query were found.', 'your-translate-domain' ); ?>
        <?php } ?>
        <?php if ($aboutPatra): ?>
            <?php include( get_stylesheet_directory() . '/template-parts/inc-block_about-patra.php'); ?>
        <?php endif; ?> 
        </div><!-- #container -->
    </main><!-- #main -->
</div><!-- #primary -->

And here are my categories: enter image description here

Any assistance in getting the pagination to function and recommendations to perform the category filtering (either through custom development OR an acceptable plugin that won't blow up my site) would be greatly appreciated.

I've tried several different ways of implementing the pagination - including using filtering scripts I found online written into the functions.php. I've even tried moving the resources archive shown here from a "custom post type" to a "page". And that obviously did not work. I've even tried a couple of pagination plugins. They did not work.

At this point I'm not sure what to do to get the pagination functioning.

As for the filtering by category, I've looked for plugins to create the function I'm looking for, and have had no luck. I'm not having much luck in sourcing the answers via the Codex or Google either.

9
  • It may be that you're overthinking this. Why not assign all 8 post types to the same categories and then just list the cateories
    – rudtek
    Sep 29 at 3:03
  • Or better yet. Only 1 post type and use categories to separate out all the posts into different "categories". You could then have one "archive" page with all the resources lined out by their categories.
    – rudtek
    Sep 29 at 4:59
  • Unfortunately, that wasn't an option. Each post type was created bc each needed its own archive. And they also wanted to be able to combine all of them on one page and then filter all of the posts by their assigned categories.
    – Mitch S
    Sep 29 at 11:25
  • categories have archive pages too
    – rudtek
    Sep 29 at 14:45
  • Then perhaps I should have built it differently, more inline with your suggestion. However, I'm currently at the point of no return. I have not only created all of the post types, and created the categories as mentioned above, but I have also migrated all of the existing content into the current infrastructure. And I'm facing a deadline of next week. So, if there's a way to do this using the current infrastructure as it is, and perform the filtering, I surely could use the assist in making it work. And even the pagination too. I'm absolutely perplexed why that's not working.
    – Mitch S
    Sep 29 at 16:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.