wp query - How to break up output of posts for different terms on same page?

admin2025-06-06  0

Background..

My site contains 9,000+ posts of custom post type "article". It has various custom taxonomies, eg. "source", "company" and (article) "type".

My taxonomy.php template file does a WP_Query for posts associated with whichever term the browser has hit, in whichever taxonomy (eg. - let's say, circa 200 posts there).

Now, here is the question...

On that page, how can I break the output posts in to segments corresponding to the article "type" taxonomy?

In other words, on the page...

  • Header:
  • Page term title: "Microsoft" (that is, tax "company" term "microsoft")
  • microsoft-specific posts for tax "type" term "news"
  • microsoft-specific posts for tax "type" term "earnings"
  • microsoft-specific posts for tax "type" term "analysis"
  • microsoft-specific posts for tax "type" term "opinion"
  • Footer

I know I could do multiple WP_Queries, but I guess I'm partly asking: is that an appropriate/the most efficient way to do it, or does it come with database overhead and is there a better way?

It seems to me that a better way may be to leave the single query in place and then to ouput the query contents in segments. Or... not... ?

I realise this may be recursive/moot where the term the browser accesses IS that for an "article", eg. , since that is all it would campaign - but I guess I'll need to cross that bridge when I come to it, eg. with a separate, taxonomy-specific.php template.

Also, some of the terms will have many posts to output, so I wonder how to handle pagination. I've already got that sorted for the one-dimensional method already in place, but pagination will bear thinking about if I am outputting things in segments and everything runs long (what exactly to paginate and how?)

Here is my taxonomy.php...

<?php



// Let's make this flexible for any custom taxonomy...

// Term:
// Get taxonomy term from page (eg. source 'beet-tv')
$term = get_queried_object();
$this_terms_name = $term->name;

// Taxonomy:
// Get taxonomy slug of this taxonomy term (eg. 'source')
$this_terms_tax_slug = $term->taxonomy;
// Standard Class Object - Source is in here.
$this_terms_tax_obj = get_taxonomy($this_terms_tax_slug);
// Get name of taxonomy using the slug (eg. 'Source')
$this_terms_tax_name = $this_terms_tax_obj->labels->singular_name;

?>



<div class="container-fluid py-3 bg-white">

     <div class="row">
            <div class="col-12">
                 <h5 class="mb-1"><?php echo $this_terms_tax_name . ': ' . $term->name; ?></h5>
            </div>
     </div>

</div>





<div class="container-fluid py-3 bg-light" style="min-height:70vh">



            <?php

            // Get posts matching for this taxonomy term
            $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
            $args = array(
                // pagination
                'nopaging' => false,
                'posts_per_page' => 40,
                'paged' => $paged,
                // taxonomies
                'tax_query' => array(
                    array(
                        'taxonomy' => $this_terms_tax_slug, // from above, whatever this taxonomy is, eg. 'source'
                        'field'    => 'slug',
                        'terms'    => $term->slug,
                    ),
                ),
            );
            // the query
            $the_query = new WP_Query($args);




            if ( $the_query->have_posts() ) : ?>

                <div class="row">
                    <div class="col-sm-12 col-md-9">
                        <div class="row">

                            <?php
                                // Post Loop!!
                                while ( $the_query->have_posts() ) {
                                    $the_query->the_post();
                                    // Tell loop template which taxonomy to loop through (set above ^)
                                    // set_query_var( 'tax_slug_to_do', $this_terms_tax_slug );
                                    // Get Loop template
                                    get_template_part('templates/loop');
                                }
                                // wp_reset_postdata();
                            ?>

                        </div>

                        <?php
                         // Pagination, if allowed above
                         if ( false === $the_query->query_vars['nopaging'] ) {
                             fellowtuts_wpbs_pagination($the_query->max_num_pages);
                         }
                         // function uses Bootstrap mark-up, cf. /
                        ?>

                    </div>
                    <div class="col-xs-12 col-sm-12 col-md-3">
                        potential sidebar
                    </div>
                </div>


            <?php else : ?>
                    Nothing here.
            <?php endif; ?>

        </div>





<?php get_footer(); ?>

And here is the loop.php file it calls...

<?php



// Generate fallback thumbnail for post:

// First, get the Source taxonomy info of this post (depends on single Source?) (eg. all 'beet-tv' info)
$this_posts_terms = wp_get_post_terms( $post->ID, 'source' );

// From those Source details, get Source term's ID (eg. 'beet-tv' is '59')
$source_id = $this_posts_terms[0]->term_taxonomy_id;

// Prefix ID with underscore (eg. '_59'?)
$source_id_prefixed = '_'. $source_id; // was $term_id_prefixed = $taxonomy .'_'. $source_id;

// Get the fallback image! ('source_image' ACF field for every Source)
$source_thumbnail_fallback = get_field( 'source_thumbnail', $source_id_prefixed );

// How to avoid generating this every time?
// Should only be called if required.

?>




<!-- column -->
<div class="d-flex align-items-stretch col-xs-12 col-sm-4 col-md-4 col-lg-3 p-2">
        <!-- Card -->
        <div class="card rounded-0 w-100 bg-white shadow-sm">
                <a href="<?php the_field( 'source_url' ); ?>">
                    <?php
                        // was: // the_post_thumbnail(null, array('class' => 'card-img-top img-responsive'));
                        if ( has_post_thumbnail($post->ID) ) {
                            // ,h_300,c_thumb,g_faces/ ?>
                            <img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' );?>" class="card-img-top img-fluid rounded-0"><!-- had w-100 -->
                            <?php
                        } else { ?>
                            <img src="<?php echo $source_thumbnail_fallback['url']; ?>" class="card-img-top img-fluid" /><!-- had w-100 -->
                        <?php }
                    ?>
                </a>
                <div class="card-body pb-2">
                        <h6 class="card-title"><a href="<?php the_field( 'source_url' ); ?>" class="text-dark"><?php the_title(); ?></a></h6>
                </div>

                <?php

                $source_terms = wp_get_post_terms( $post->ID, 'source' );


                ?>

                <div class="card-footer bg-white text-muted small pt-0 pb-1 border-top-0 px-3">
                    <?php echo get_avatar( get_the_author_meta( 'user_email' ), '17', null, null, array( 'class' => array( 'rounded-circle' ) ) ); ?>
                    <span class="ml-1"><?php echo get_the_author(); ?></span>
                </div>
                <div class="card-footer bg-white text-muted small pt-0 border-top-0 px-3">

                        <?php

                        foreach($source_terms as $source_single) {

                                // Icon
                                $source_icon = get_field( 'source_icon', $source_id_prefixed );
                                if ( $source_icon ) {
                                        $favicon = $source_icon['url'];
                                } else {
                                        $favicon = '='. get_field( 'source_url' );
                                }
                                ?>


                            <img src="<?php echo $favicon; ?>" width="17" class="rounded-circle">
                            <span class="ml-1"><a href="/source/<?php echo $source_single->slug ?>/" class="text-muted"><?php echo $source_single->name;?></a></span>

                         <?php }; ?>


                </div>
            <!--
            <div class="card-footer bg-white text-muted small pt-0 border-top-0">
                    <?php echo get_the_date( 'M j, Y' ); ?>
            </div>
            -->
        </div>
        <!-- Card -->
</div>
<!-- column -->
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749212313a317303.html

最新回复(0)