最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Loop multiple taxonomy in custom post

matteradmin5PV0评论

I have a custom post called "rock" and taxonomy "genres" i need to filter the posts with tag or something like

post_type > taxonomy > taxonomy_tag (tagsss)

I can not find a solution :(

i have register a new tag taxonomies in the fuctions (like tag) (is not whether it is a good choice)

    add_action( 'init', 'create_tag_taxonomiess', 0 );
    function create_tag_taxonomiess() 
    {
      // Add new taxonomy, NOT hierarchical (like tags)
      $labels = array(
        'name' => _x( 'Tags', 'taxonomy general name' ),
        'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Tags' ),
        'popular_items' => __( 'Popular Tags' ),
        'all_items' => __( 'All Tags' ),
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => __( 'Edit Tag' ), 
        'update_item' => __( 'Update Tag' ),
        'add_new_item' => __( 'Add New Tag' ),
        'new_item_name' => __( 'New Tag Name' ),
        'separate_items_with_commas' => __( 'Separate tags with commas' ),
        'add_or_remove_items' => __( 'Add or remove tags' ),
        'choose_from_most_used' => __( 'Choose from the most used tags' ),
        'menu_name' => __( 'Tags' ),
      ); 

      register_taxonomy('tagsss','rock',array(
        'hierarchical' => false,
        'labels' => $labels,
        'show_ui' => true,
        'update_count_callback' => '_update_post_term_count',
        'query_var' => true,
        'rewrite' => array( 'slug' => 'tag' ),
      ));

}

my loop in taxonomy-genres-events.php

<?php $args = array( 'post_type' => 'rock','taxonomy' => 'genres','tagsss' =>'concerts', 'orderby' => 'menu-order' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $rock; ?>

 <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php endwhile;?>

EDIT:

I have found the solution by myself

        <?php
$args = array(
    'tax_query' => array(
        array(

            'posts_per_page' => 5,
            'order' => 'DESC',
            'taxonomy' => 'tagsss',
            'field' => 'slug',
            'terms' => 'concerts',

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

I have a custom post called "rock" and taxonomy "genres" i need to filter the posts with tag or something like

post_type > taxonomy > taxonomy_tag (tagsss)

I can not find a solution :(

i have register a new tag taxonomies in the fuctions (like tag) (is not whether it is a good choice)

    add_action( 'init', 'create_tag_taxonomiess', 0 );
    function create_tag_taxonomiess() 
    {
      // Add new taxonomy, NOT hierarchical (like tags)
      $labels = array(
        'name' => _x( 'Tags', 'taxonomy general name' ),
        'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Tags' ),
        'popular_items' => __( 'Popular Tags' ),
        'all_items' => __( 'All Tags' ),
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => __( 'Edit Tag' ), 
        'update_item' => __( 'Update Tag' ),
        'add_new_item' => __( 'Add New Tag' ),
        'new_item_name' => __( 'New Tag Name' ),
        'separate_items_with_commas' => __( 'Separate tags with commas' ),
        'add_or_remove_items' => __( 'Add or remove tags' ),
        'choose_from_most_used' => __( 'Choose from the most used tags' ),
        'menu_name' => __( 'Tags' ),
      ); 

      register_taxonomy('tagsss','rock',array(
        'hierarchical' => false,
        'labels' => $labels,
        'show_ui' => true,
        'update_count_callback' => '_update_post_term_count',
        'query_var' => true,
        'rewrite' => array( 'slug' => 'tag' ),
      ));

}

my loop in taxonomy-genres-events.php

<?php $args = array( 'post_type' => 'rock','taxonomy' => 'genres','tagsss' =>'concerts', 'orderby' => 'menu-order' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $rock; ?>

 <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php endwhile;?>

EDIT:

I have found the solution by myself

        <?php
$args = array(
    'tax_query' => array(
        array(

            'posts_per_page' => 5,
            'order' => 'DESC',
            'taxonomy' => 'tagsss',
            'field' => 'slug',
            'terms' => 'concerts',

        )
    )
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();?> 
Share Improve this question edited Dec 9, 2014 at 22:18 user3766356 asked Dec 9, 2014 at 21:03 user3766356user3766356 131 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You cannot natively query broadly by taxonomy, only by terms

If you study through Taxonomy Parameters in Codex it is now pretty easy to construct conditions for multiple sets of terms required.

Still it's not completely flexible, I won't know easy way to do things like “having any term in some taxonomy” on top of my head.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far