admin管理员组

文章数量:1410674

I have an archive page that I am building multi-filtering for. The archive page lists paginated results from a custom post type, and there are 3000 posts within it.

I would like to be able to filter this post type by two taxonomies, but importantly the page should only show an option to filter by one of the categories within each taxonomy if there is a result.

The below code works, but is extremely slow and inefficient, taking five seconds to return results. How can I improve this or are there alternative solutions to implement multi-filtering?

$tax_args = array(
    'post_type' => 'output',
    'post_status' => 'publish',
    'posts_per_page' => '2000',
);

$the_query = new WP_Query($tax_args);
$all_terms = array();

if($the_query->have_posts()):
    while($the_query->have_posts()):
    $the_query->the_post(); 

        $terms = wp_get_post_terms($the_query->post->ID, array('library-topics', 'library-types'));
        foreach ( $terms as $term ) {
            $all_terms[] = $term->name;
        }

    endwhile;   
    $result = array_unique($all_terms);
    foreach ($result as $term){
        echo $term . '<br/>';
    }
endif;
wp_reset_postdata();

本文标签: filtersList all categories that have results in a query