简体   繁体   中英

WordPress custom taxonomy query

I have a custom post type (Media Articles) which uses some custom fields and has one custom taxonomy (Media Article Category) and 14 terms within that taxonomy. I have no problem outputting all custom posts using a CPT archive template.

I also have a menu in the sidebar which lists the terms for the purpose of filtering the custom posts (I used a custom WP menu widget to make the list).

I have a custom taxonomy template set up with a query. I am able to successfully display the posts either in combination or on their own depending on what taxonomy term is placed in the args. So it's working as expected - IF I didn't need to use the filtering menu. But when I try to use the menu of taxonomy terms, every menu item displays the same thing.

What I need to do is have EACH taxonomy term in that menu display ONLY that term's CPTs. Yes, I could make one template for each term (all 14 of them!), but where's the fun in that? Also, the client will likely be adding more terms on the fly so it would need to accommodate newly generated ones.

In essence, I would like to have one query/template for all taxonomy terms that would dynamically display the posts for whichever taxonomy term is clicked.

Can anyone help me push this to the next level?

Here is the display currently:

screenshot of current display with sidebar of custom taxonomies

Here is my current query:

$date = get_field('pub_date', false, false);
$date = new DateTime($date);

$args = array(
'post_type' => 'media_articles',
'post_status' => 'publish',
  'posts_per_page' => '10',
'meta_key'          => 'media_publication_date',
'orderby'           => 'meta_value',
'order'             => 'DESC',
'tax_query' => array(
    'relation' => 'OR',
    array(
        'taxonomy' => 'media-article-category',
        'field'    => 'slug',
        'terms'    => array( 'focus-on-the-lands' ),  //
    ),
    array(
        'taxonomy' => 'media-article-category',
        'field'    => 'slug',
        'terms'    => array( 'the-local-story' ),  //
    ),
),

);

$query = new WP_Query( $args );
if ( $query->have_posts() ) :
    while ( $query->have_posts() ) : $query->the_post();

    ?>

            <div class="media-item">
                <h2><a href="<?php the_field('media_url'); ?>" target="_blank"><?php the_title(); ?><i class="fa fa-external-link" aria-hidden="true"></i></a></h2>
                <?php 
                echo '<p class="lol-media-date">Publication Date: ' . DateTime::createFromFormat('Ymd', get_field('media_publication_date'))->format('F j, Y') . '</p>';
                echo '<p>' . get_the_term_list( $post->ID, 'media-article-topics', 'Tags: ', ', ' ) . '</p>';
                echo '<p class="lol-media-description">' . get_field('media_description') . '<span class="lol-media-publication">' . get_field('media_publication') . '</span></p>';
                ?>

            </div><!-- end .media-item -->


        <?php

    endwhile;
    endif;

I am using the Genesis Framework.

When you click a taxonomie-menu-item, then wordpress filters posts by default. I think you should not use your own query here, but you can modify the default query.

Use the action hook pre_get_posts to alter the main query.

function my_media_article_category_query( $query ) {
    if ( is_tax( 'media-article-category' ) ) {
        $query->set( 'meta_key', 'media_publication_date' );
        $query->set( 'orderby', 'meta_value' );
        $query->set( 'order', 'DESC' );
    }
}
add_filter( 'pre_get_posts', 'my_media_article_category_query' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM