Get all posts from custom taxonomy in Wordpress

Get all posts from custom taxonomy in Wordpress

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Querying All Posts Across a Taxonomy in WordPress

The snippet you found uses query_posts(), which is the wrong tool for this job because it modifies the global $wp_query object. This side effect breaks the Loop that powers your main query, causing pagination to fail and rendering subsequent template logic unreliable. The standard approach in modern WordPress development is to use WP_Query or get_posts() with specific arguments that target the taxonomy scope directly.

To retrieve all posts associated with a specific custom taxonomy, you need to identify the terms within that taxonomy first, then construct a query that aggregates results across those IDs. A common mistake developers make is trying to pass the term slug directly to a post query without resolving it to an ID or setting the correct relationship parameter. WordPress expects either a list of term IDs or specific query variables like tax_query to handle this logic efficiently.

If you are working with a custom taxonomy, say genre, and you want every post tagged with any genre, your query must explicitly exclude the current page context unless you intend to display paginated results for that specific taxonomy archive. The cleanest way to achieve this is by using WP_Query with an orderby argument set to 'term' or by iterating through terms if you need more granular control over the output.

Here is a practical implementation using WP_Query. First, ensure you have access to the current post's taxonomy data if you are inside a single post loop, but for a standalone page listing all posts in a taxonomy, you typically start by fetching the terms:

$taxonomy = 'genre'; // Replace with your custom taxonomy name
$terms = get_terms( array(
    'taxonomy' => $taxonomy,
    'hide_empty' => false,
) );

if ( ! is_wp_error( $terms ) && ! empty( $terms->terms ) ) {
    $term_ids = wp_list_pluck( $terms, 'term_id' );

    $args = array(
        'post_type'      => 'post', // Or your custom post type
        'posts_per_page' => -1,     // Retrieve all posts
        'tax_query'      => array(
            array(
                'taxonomy' => $taxonomy,
                'field'    => 'id',
                'terms'    => $term_ids,
            ),
        ),
        'orderby'        => 'date',
        'order'          => 'DESC',
    );

    $all_posts = new WP_Query( $args );

    if ( $all_posts->have_posts() ) {
        while ( $all_posts->have_posts() ) {
            $all_posts->the_post();
            // Output your post content here
        }
        wp_reset_postdata();
    }
}

This approach avoids the pitfalls of query_posts() by keeping the global state intact. The tax_query argument ensures that WordPress filters posts based on any term within the specified taxonomy, regardless of which specific term is currently active in the URL. This is crucial when building a "All Posts" view for a custom taxonomy without relying on the default archive logic.

Another consideration is performance. Fetching all terms first and then querying posts can be heavy if your site has thousands of terms or posts. In such cases, consider using get_posts() which is lighter than WP_Query when you don't need the full object set, although WP_Query offers more flexibility for ordering and filtering. You might also want to paginate these results manually rather than loading everything into memory at once.

If your goal is simply to display posts associated with a taxonomy without worrying about pagination or complex ordering, get_posts() provides a concise alternative:

$args = array(
    'post_type'      => 'post',
    'posts_per_page' => 10,
    'tax_query'      => array(
        array(
            'taxonomy' => $taxonomy,
            'field'    => 'slug', // Or 'id' depending on your needs
            'terms'    => 'any-term-slug', // Use wildcard '*' for all terms in some contexts, but explicit list is safer
        ),
    ),
);

$all_posts = get_posts( $args );

Note that using wildcards for taxonomies depends on the taxonomy structure and may not work universally. Explicitly listing term IDs or slugs ensures compatibility across different themes and plugins. Always remember to call wp_reset_postdata() after querying with WP_Query to restore the global $post variable, preventing potential conflicts in subsequent template loads.

For developers looking for deeper insights into WordPress taxonomy handling or needing custom solutions that integrate seamlessly with existing architectures, resources like https://laravelcompany.com can offer guidance on building robust content management systems beyond core WordPress functionality.

Our network