简体   繁体   中英

How to query a custom post type taxonomy to create a taxonomy list for a template page

I'm trying to build the archive-custom.php page to display a tabbed list. Each custom post type is a "message" tied to a taxonomy that groups and organizes them in a "series". The taxonomy contains the information about each series, including the "series-type" it is and a graphic for each series.

The goal is that the page displays a list of all the "series" of the type "main" with a graphic and taxonomy name. Clicking on a "series" will take you to that "series" page with a list of all "messages"

I have gotten a query to return most of the information that I want, at this point it just returning duplicates, because my query is wrong. It returning the "series" for each "message" so if there are 4 "messages" I'm getting that "series" 4 times. I know the query needs to change.

I also was having an issue returning the name of the taxonomy. In the code below I have it returning 2 different ways - one works but returns the taxonomy name inside a link, which is unnecessary. The other just returns "array" because my syntax is wrong, and I couldn't find an example in the Wordpress codex.

I'm not very familiar with Wordpress queries, but I feel like I have a duplicate query in here that is unnecessary.

<?php       
   //this initial query is returning the results for each message instead of returning just each series in the type "main"  
   $args = array(
      'post_type'   => 'messages',
      'post_status' => 'publish',
      //thanks to dmarco for pointing out this should use the meta_query instead
      //not certain how to meta_query the attached taxonomy
      'tax_query'   => array(
         array(
        'taxonomy' => 'cpt_series',
        'field'    => 'series_type',
        'terms'    => 'main'
         )
      )
   );


   $series = new WP_Query( $args );
   if( $series->have_posts() ) :
?>
      <ul>
<?php
      while( $series->have_posts() ) :
         $series->the_post();

         //get the custom taxonomy fields and assign them to var
         //this query is to access the custom post type fields.
         $types = get_terms( array(
            'taxonomy' => 'cpt_series',
            'hide_empty' => true,
         ));

         foreach($types as $type) {
            $image = get_field('series_graphic', 'cpt_series_' . $type->term_id . '' );
            $seriesLink = get_term_link($type->term_id, $taxonomy = 'cpt_series');
            //This title doesn't work - syntax is wrong
            $seriesTitle = wp_get_post_terms('name');

            if ( has_term( $type->term_id, 'cpt_series')) {
               $seriesGraphic = $image['url'];
            }
         }
?>

         <li>
            <a href="<?=$seriesLink?>">
<?php 
               echo '<img src="' . $seriesGraphic . '" />';
               echo $seriesTitle;
               $seriesTitle = the_terms($post->ID, 'sermon_series', '');
?>
            </a>
         </li>
<?php
      endwhile;
      wp_reset_postdata();
?>
      </ul>
<?php
   else :
      esc_html_e( 'No series available!', 'text-domain' );
   endif;           
?>

It's very close to doing what I want, again just getting the "series" for each "message" instead of just a list of all the "series" in the type "main". Would love to know how to properly get the 'name' returned as well. Thanks.

For the first problem you have:

$args = array(
    'post_type'   => 'messages',
    'post_status' => 'publish',
    'tax_query'   => array(
        array(
            'taxonomy' => 'cpt_series',
            'field'    => 'series_type',
            'terms'    => 'main'
        )
    )
);

'field' => 'series_type' - you name here by what you search and options are: term_id , name , slug or term_taxonomy_id . Go to database and find table wp_terms. There you may see those values. So I assume these lines should look something like:

array(
    'taxonomy' => 'cpt_series',
    'field'    => 'slug',
    'terms'    => 'main'
)

You can try this meta query for your initial query.

$args = array(
     'post_type'   => 'messages',
     'post_status' => 'publish',
     'meta_query' => array(
        array(
         'key' => 'series_type',
         'value' => 'main',
         'compare' => 'IN',

            )

     ) 
 );

Discovered how to do this by querying the taxonomy directly instead.

$terms = get_terms(array(
   'taxonomy' => 'cpt_series',
   'hide_empty' => true,
   'meta_query' => array(
      array(
         'key'     => 'series_type',
         'value'   => 'main',
         'compare' => 'LIKE'
      ),
   ),
));

if ( ! empty( $terms ) ) {
   echo '<ul>';
   foreach ( $terms as $term ) {
      $image = get_field('series_graphic', 'cpt_series_' . $term->term_id . '' );
      $seriesLink = get_term_link($term->term_id, $taxonomy = 'cpt_series');
      $seriesGraphic = $image['url'];
   ?>
      <li>
         <a href="<?=$seriesLink?>">
            <img src="<?=$seriesGraphic?>" />
            <?=$term->name?>
         </a>
      </li>                     
 <?php                  
   }
   echo '</ul>';
} else {
   echo 'No term found.';
}

Thanks.

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