WordPress.org

Ready to get started?Download WordPress

Forums

Displaying and Separating Custom Posts by Taxonomy (2 posts)

  1. itrogers
    Member
    Posted 1 year ago #

    I have created a custom post type called locations.

    I have one custom taxonomy called cities.

    There are 4 cities named portland, phoenix, tucson, and bullhead-city.

    Is there a way I can query these custom posts, and separate these custom posts into columns with their corresponding cities?

    Kinda new to query strings and custom posts. Thanks!

  2. wpismypuppet
    Member
    Posted 1 year ago #

    This isn't tested, but it's along the idea you are looking for...

    $post_type = 'locations';
    
    // Get all the taxonomies for this post type
    $taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
    
    foreach( $taxonomies as $taxonomy ) : 
    
        // Gets every "category" (term) in this taxonomy to get the respective posts
        $terms = get_terms( $taxonomy->name );
    
        foreach( $terms as $term ) : 
    
            $posts = new WP_Query( array( 'taxonomy' => $taxonomy, 'term' => $term->slug, 'posts_per_page' => -1 ) );
    
            if( $posts->have_posts() : while( $posts->have_posts() ) : $posts->the_post();
                //Do you general query loop here
            endwhile; endif;
    
        endforeach;
    
    endforeach;

Topic Closed

This topic has been closed to new replies.

About this Topic