skip to Main Content

Fairly new to wordpress, but Im trying to do what should be fairly straight forward. I want to get a list of all "departments" (a custom taxonomy). and then all the posts in those departments. A post can be in more than one department.

All the examples I have found seems to get the departments then loop through them and get the posts, but this seems like an inefficient way to query the DB.

So my end result would hopefully look like this:

[
    "Department 1" => [
        0 => post 1,
        1 => post 2,
        2 => post 3
    ],
    "Department 2" => [
        0 => post 1,
        1 => post 2,
        2 => post 4
    ],
]

Note, some posts appearing under multiple departments.

Can anyone point me in the right direction?

Thanks in advance

2

Answers


  1. Please try with this code. May is useful for you. Thanks

    $list_custom_taxonomy = get_terms('your_custom_taxonomy'); // your taxonomy name
    
    foreach($list_custom_taxonomy as $custom_single_term) {
        $args = array(
            'post_type' => 'your_custom_post_type', // your CPT name
            'tax_query' => array(
                array(
                    'taxonomy' => 'your_custom_taxonomy', // your taxonomy name
                    'field' => 'slug',
                    'terms' => $custom_single_term->slug,
                ),
            ),
        );
        $post_list = new WP_Query($args);
        if($post_list->have_posts()) {
            echo '<h1>'.$custom_single_term->name.'</h1>';
            while($post_list->have_posts()) { $post_list->the_post();
                echo '<h6>'.get_the_title().'</h6>';
            }
        }
        wp_reset_postdata();
    }
    
    Login or Signup to reply.
  2. You use the posts_clauses filter hook and modify the SQL query based on your requirements. check below code.

    function orderby_tax_grouped_clauses( $clauses, $wp_query ) {
        global $wpdb;
        $taxonomy = 'your_custom_taxonomy';
            $clauses['join'] .= "
                LEFT OUTER JOIN {$wpdb->term_relationships} AS rel2 ON {$wpdb->posts}.ID = rel2.object_id
                LEFT OUTER JOIN {$wpdb->term_taxonomy} AS tax2 ON rel2.term_taxonomy_id = tax2.term_taxonomy_id
                LEFT OUTER JOIN {$wpdb->terms} USING (term_id)
            ";
            $clauses['where'] .= " AND (taxonomy = '".$taxonomy."' OR taxonomy IS NULL)";
            $clauses['groupby'] = "rel2.object_id";
            $clauses['orderby']  = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC) "; // 'ASC' OR 'DESC';
        return $clauses;
    }
    add_filter( 'posts_clauses', 'orderby_tax_grouped_clauses', 10, 2 );
    
    $args = array(
        'post_type' => 'your_custom_post_type', // your CPT name
        'posts_per_page' => -1
    );
    
    add_filter('posts_clauses', 'orderby_tax_grouped_clauses', 10, 2 );
    
    $post_list = new WP_Query( $args );
    
    remove_filter('posts_clauses', 'orderby_tax_grouped_clauses', 10, 2 );
    

    and remove filter so it’s only apply to your query not all query.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search