0

Is there a way to get posts from a custom taxonomy without specifying a term?

I have a custom taxonomy called media_category and I want to get all the attachments which use this taxonomy since I have some attachments which doesnt use this taxonomy at all.

4 Answers 4

1

You may do it by this method.




$media_category = get_terms('media_category'); foreach($media_category as $cat) { wp_reset_query(); $args = array('post_type' => 'your_post_type', 'tax_query' => array( array( 'taxonomy' => 'media_category', 'field' => 'slug', 'terms' => $cat->slug, ), ), ); $loop = new WP_Query($args); if($loop->have_posts()) { // echo $cat->name; while($loop->have_posts()) : $loop->the_post(); echo get_the_title(); endwhile; } }

0
1
<?php
$args = array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'taxonomy_name',
            'field' => 'id',
            'terms' => '22'
        )
    )
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
    //content
endwhile;
?>

more check out here about Taxonomy Parameters

1
  • But you are passing 'term_id = 22' in this query, i need all the terms
    – Broshi
    Jan 4, 2017 at 10:04
0

You need to create a taxonomy archive by doing something like this:

1) In the theme directory create a file named taxonomy-media_category.php with the archive loop like...

<?php
get_header(); ?>

<div id="container">
    <div id="content" role="main">

        <?php the_post(); ?>
        <h1 class="entry-title"><?php the_title(); ?></h1>

        <?php get_search_form(); ?>

        <h2>Archives by Month:</h2>
        <ul>
            <?php //SOME OF YOUR CODE HERE ?>
        </ul>

    </div><!-- #content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

2) In the loop, you can change the code as needed

0

Eventually I used this:

$taxonomy = 'MY_TAXONOMY_HERE';

// Get all the terms of that taxonomy
$terms = get_terms( $taxonomy, 'orderby=count&hide_empty=1' );

$args = array_merge( $args, [
    'tax_query' => array(
        array(
            'taxonomy' => $taxonomy,
            'field' => 'id',
            'terms' => wp_list_pluck( $terms, 'term_id' )
        )
    )
]);

$query = new WP_Query( $args );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.