get posts within get_terms loop - wordpress

I have a basic loop set up to display all terms in a custom taxonomy.
<?php
$workshops = get_terms( 'workshop', array(
'orderby' => 'name',
'hide_empty' => 0,
) );
foreach ( $workshops as $workshop ) { ?>
<h3><?php echo $workshop->name; ?></h3>
<?php echo term_description($workshop); ?>
<?php } ?>
How can I display all posts for each respective term within that loop?
For example..
Taxonomy is Movies. Terms are Comedy, Horror, etc.
I'd like output to be
Comedy
Description for comedy term
Movie 1
Movie 2
Horror
Description for horror term
Movie 3
Movie 4
Thanks! Rich

First off, you are using a deprecated version of get_terms so we should fix that first:
$workshops = get_terms( array(
'taxonomy' => 'workshop',
'orderby' => 'name',
'hide_empty' => false
) );
Then, within your term loop, you need to create another query to grab all the posts that fall under the term:
$query = new WP_Query( array(
'post_type' => 'post', // Or your custom post type's slug
'posts_per_page' => -1, // Do not paginate posts
'tax_query' => array(
array(
'taxonomy' => 'workshop',
'field' => 'term_id',
'terms' => $workshop->term_id
)
)
) );
Finally, still within your term loop, write another loop to build the lists of posts:
<?php if ( $query->have_posts() ): ?>
<ul class="term-post-list" id="term-<?php echo $workshop->term_id; ?>-posts">
<?php while ( $query->have_posts() ): $query->the_post(); ?>
<li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_title(); ?>
</li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
<?php endif; ?>

Related

how display post on subcategory

I want to display posts in the subcategory but don't know what to do. But this code does not show the post in the category but the entire post in the category. Can you help me?
<?php
$query = new WP_Query( array(
'post_type' => 'hotcheck',
'topics__in' => array($cat->term_id),
) );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="entry">
<h2 class="title"><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<!-- show pagination here -->
<?php else : ?>
Please try below code
$args = array(
'post_type' => 'hotcheck',
'tax_query' => array(
array(
'taxonomy' => 'category', // Add your taxonomy name here
'field' => 'term_id',
'terms' => $cat->term_id,
),
),
);
$query = new WP_Query($args);
Hope its work for you Thanks!

Custom Post Type showing even when taxonomy is blank

I am using Custom Post Type to get its contents by using Custom Taxonomy in different pages.
However, the contents are being displayed even if the post type has no taxonomy.
P.S. I am using ACV as well to get the fields of Taxonomy
Here's the code:
<?php if( get_field('review_category_name') ):
$taxonomy_slug = get_field('review_category_name'); //Advanced Custom Field here
endif; ?>
<?php $args = array( 'post_type' => 'reviews','tax_query' => array(
'taxonomy' => 'reviews_category',
'field' => 'slug',
'terms' => $taxonomy_slug,
), 'posts_per_page' => 5 );
$loop = new WP_Query( $args ); ?>
<div class="">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
Am I missing anything here?
$taxonomy_slug is a variable to get the custom post type slug.
Wrap everything in the get_field('review_category_name') if check:
<?php if( get_field('review_category_name') ): ?>
<?php
$taxonomy_slug = get_field('review_category_name'); //Advanced Custom Field here
$args = array( 'post_type' => 'reviews','tax_query' => array(
'taxonomy' => 'reviews_category',
'field' => 'slug',
'terms' => $taxonomy_slug,
), 'posts_per_page' => 5 );
$loop = new WP_Query( $args );
?>
<div class="">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
PS your example was missing the endwhile

Show only post of custom category

I have a custom post types called photo gallery. In that post type I have registered a taxonomy called video. Then i have created two category under video called 'personal' & 'commercial'. Now i want to show only the commercial category post to a page section. How can I do that? Here is the code I have tried but not working
<?php
$args = array(
'post_type'=>'photo_gallerys',
'post_status'=>'publish',
'video'=>'commercial',
'posts_per_page'=>-1,
'paged'=>get_query_var('paged')
);
// the query
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Try this code hope you'll find your solution
<?php
$tax_post_args = array(
'post_type' => 'your post type name',
'posts_per_page' => 999,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'your taxonomy name',
'field' => 'id',
'terms' => your category id
)
)
);
$tax_post_qry = new WP_Query($tax_post_args);
while($tax_post_qry->have_posts()) :
$tax_post_qry->the_post();
the_title();
endwhile;
?>
if you want to get posts from category slug then use this code in tax_query array
'field' => 'slug',
'terms' => 'your category slug'
use 'cat' => "your_cat_id in your wp query argument.
for example if I want to display post for only commercial category and commercial have category id 21, then I will write query to display post like this:
<?php
$args = array(
'post_type'=>'photo_gallerys',
'post_status'=>'publish',
'video'=>'commercial',
'cat' => 21,
'posts_per_page'=>-1,
'paged'=>get_query_var('paged')
);

Multiple wordpress loops for custom post type & taxonomy with no duplicates

I'm trying to make a custom template to display multiple loops from the same custom post type, but different categories.
Here's what I am after:
From custom post type: 'Portfolio'
In custom category 1 'Music':
1 featured post at top
Music Heading
3 sub-featured posts
12 posts (title only)
In custom category 2 'Presenters':
- Presenters Heading
- 3 posts
In custom category 3 'News':
- News Heading
- 3 posts
Here's the code I am working with:
<?php if (have_posts()) : while (have_posts()) : the_post(); //WP loop ?>
<?php the_content(); ?>
<?php $args=array( //Loop 1
'post_type' => 'dt_portfolio',
'taxonomy' => 'dt_portfolio_category',
'term' => 'music',
'posts_per_page' => 16
);
$myloop = new WP_Query($args);
if($myloop->have_posts()) : while($myloop->have_posts()) :
$myloop->the_post();
?>
<!--the content -->
<?php endwhile; endif; ?>
<?php wp_reset_query(); // end music loop ?>
<h2>Presenters</h2>
<?php $args=array( //Loop 2
'post_type' => 'dt_portfolio',
'taxonomy' => 'dt_portfolio_category',
'term' => 'presenters',
'posts_per_page' => 3
);
$myloop = new WP_Query($args);
if($myloop->have_posts()) : while($myloop->have_posts()) :
$myloop->the_post();
?>
<!--the content -->
<?php endwhile; endif; ?>
<?php wp_reset_query(); // end presenters loop ?>
<h2>News</h2>
<?php $args=array( //Loop 3
'post_type' => 'dt_portfolio',
'taxonomy' => 'dt_portfolio_category',
'term' => 'news',
'posts_per_page' => 3
);
$myloop = new WP_Query($args);
if($myloop->have_posts()) : while($myloop->have_posts()) :
$myloop->the_post();
?>
<!--the content -->
<?php endwhile; endif; ?>
<?php wp_reset_query(); // end news loop ?>
<?php endwhile; endif; // end WP loop?>
Overall the 3 loops work great.
The part I need help on is the 1st loop section. I need to take all 16 posts from the same custom taxonomy 'dt_portfolio_category' -> 'music'. But break them into a 1 top featured post (full-width), then a heading, then 3 sub-featured posts (3 columns), then 12 posts with just the title (3 columns). I have tried to break it into 3 separate loops, but the content gets duplicated... and I figure there must be a cleaner way to do it.
Thank You!
dude work out with this:
$args=array( //Loop 3
'post_type' => 'dt_portfolio',
'tax_query' => array(
array('taxonomy'=>'dt_portfolio_category',
'term'=> 'news',
'field'=>'slug')
),
'posts_per_page' => 3
);
rest is with your code... no changes.
Hope it will work
<?php
$args=array(
'post_type' => 'dt_portfolio',
'taxonomy' => 'dt_portfolio_category',
'term' => 'music',
'posts_per_page' => 16
);
$music = new WP_Query($args);
$counter = 1;
if($music->have_posts()) : while($music->have_posts()) :
$music->the_post();
if ($counter = 1){
# code...
# I'd use some helper functions here
print_main_music();
} elseif ($counter > 1 && $counter < 5) {
# code...
# I'd use some helper functions here
print_featured_music();
} else {
# code...
# I'd use some helper functions here
print_other_music();
}
$counter++;
endwhile; endif;
?>
The helper functions have to be in your functions.php file and they have to echo-ing simply your HTML with template tags (the_content(), the_title(), etc...); I think you are not asking for the entire HTML+CSS layout, right?
Obviously you can put the HTML mixed with the PHP...that is not such good, but for testing purpose it is just ok.
The design parameters have changes slightly. I have come up with a solution that is working to show:
1 full width news item
3 news with excerpt
1 full width music item
16 music items with image and title
3 posts from a misc category
3 posts from a different misc category
For the content in each section I am using- get_template_part.
Here's what is working:
Start with one loop to show the 1st full width news item:
<?php
$args=array(
'post_type' => 'dt_portfolio',
'taxonomy' => 'dt_portfolio_category',
'term' => 'news',
'posts_per_page' => 1
);
$fullnewsloop = new WP_Query($args);
if($fullnewsloop->have_posts()) : while($fullnewsloop->have_posts()) :
$fullnewsloop->the_post();
get_template_part( 'content-full-width', get_post_format() );
endwhile; endif; ?>
Use a second loop to show the next 3 news items. Offset is the key for skipping the first news item that has already been displayed in the fullnewsloop.
<?php
$args=array(
'post_type' => 'dt_portfolio',
'taxonomy' => 'dt_portfolio_category',
'term' => 'news',
'posts_per_page' => 3,
'offset' => 1 // this skips the first post from the news category.
);
$shortnewsloop = new WP_Query($args);
if($shortnewsloop->have_posts()) : while($shortnewsloop->have_posts()) :
$shortnewsloop->the_post();
get_template_part( 'content-title-excerpt', get_post_format() );
endwhile; endif; ?>
The next section recycles the above loops using different taxonomy terms.
<?php
$args=array (
'post_type' => 'dt_portfolio',
'taxonomy' => 'dt_portfolio_category',
'term' => 'music',
'posts_per_page' => 1
);
$fullmusicloop = new WP_Query($args);
if($fullmusicloop->have_posts()) : while($fullmusicloop->have_posts()) :
$fullmusicloop->the_post();
get_template_part( 'content-full-width', get_post_format() );
endwhile; endif; ?>
<?php
$args=array(
'post_type' => 'dt_portfolio',
'taxonomy' => 'dt_portfolio_category',
'term' => 'music',
'posts_per_page' => 16,
'offset' => 1 // this skips the post already displayed in the fullmusicloop.
);
$shortmusicloop = new WP_Query($args);
if($shortmusicloop->have_posts()) : while($shortmusicloop->have_posts()) :
$shortmusicloop->the_post();
get_template_part( 'content-title-image', get_post_format() );
endwhile; endif; ?>
The last section is two more loops from taxonomy terms.
<?php
$args=array(
'post_type' => 'dt_portfolio',
'taxonomy' => 'dt_portfolio_category',
'term' => 'speakerss',
'posts_per_page' => 3,
);
$speakersloop = new WP_Query($args);
if($speakersloop->have_posts()) : while($speakersloop->have_posts()) :
$speakersloop->the_post();
get_template_part( 'content-title-image', get_post_format() );
endwhile; endif; ?>
<?php
$args=array(
'post_type' => 'dt_portfolio',
'taxonomy' => 'dt_portfolio_category',
'term' => 'artists',
'posts_per_page' => 3,
);
$artistsloop = new WP_Query($args);
if($artistsloop->have_posts()) : while($artistsloop->have_posts()) :
$artistsloop->the_post();
get_template_part( 'content-title-image', get_post_format() );
endwhile; endif; ?>

Cant' Display Custom Post Type Title Base on Taxonomy Terms

Trying to list all Custom Post Type titles based on filtered Custom Taxonomy Terms I am getting the list of all post titles instead of getting the list of Queried post. Here is the code I am using:
<?php
$loop = new WP_Query(
array(
'post_type' => 'photos',
'technique' => 'zevar',
'post_child' => 0,
'posts_per_page' => 10
)
);
?>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
As you can see I have a Custom Post Type called "photos" and Custom Taxonomy registered as "technique". Under Taxonomy "technique" I have some terms which one of them is "zevar". Can you please let me know what I am doing wrong here?
Your taxonomy query is not correct. check following code.
<?php
$loop = new WP_Query(
array(
'post_type' => 'photos',
'tax_query' => array(
array(
'taxonomy' => 'technique',
'field' => 'slug',
'terms' => 'zevar'
)
),
'posts_per_page' => 10
)
);
?>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
Reference on how to use taxonomy parameter https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

Resources