Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to show post w/ custom taxonomy or latest post from category WordPress

  • SOLVED

I am setting up a gallery where for each category I want to show 1 post that <strong>either</strong> has a specific term ( from a custom taxonomy) or if no posts with term exists then the latest post within that category should show.

Here is the code I have found so far that works to pull the latest post per category. I believe I need to check for the term existing first, but I am not sure where to add the additional query to check whether the term exists.



<?php
//for each category, show one post
$cat_args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories=get_categories($cat_args);
foreach($categories as $category) {

$args=array(
'showposts' => 1,
'category__in' => array($category->term_id),
'caller_get_posts'=>1
);
//get the posts with the conditions in $args
$posts=get_posts($args);

//if there are posts setup the post and format below
if ($posts) {
foreach($posts as $post) {

setup_postdata($post);

?>

<div class="post">
<?php the_post_thumbnail('thumbnail', array( 'class' => 'alignleft' , 'style' => 'border-top: none;' )); ?>

<?php echo ' View all items from <a href=" ' . get_category_link( $category->term_id )' '">' ?>

<div>

</div><!-- #post-## -->
<?php

} // foreach($posts

} // if ($posts
} // foreach($categories
?>



Answers (3)

2011-07-24

Dylan Kuhn answers:

Here's some code to try. You'd replace 'my_taxonomy' and 'My Term' with your values. Not tested, but hopefully close:


<?php
//for each category, show one post

$cat_args=array(
'orderby' => 'name',
'order' => 'ASC'
);

$categories=get_categories($cat_args);

foreach($categories as $category) {

$no_term_args = array(
'posts_per_page' => 1,
'category__in' => array($category->term_id),
'ignore_sticky_posts' => true
);
$with_term_args = $no_term_args;
$with_term_args['tax_query'] = array(
array(
'taxonomy' => 'my_custom_tax',
'field' => 'name',
'terms' => 'My Term'
)
);

$cat_query = new WP_Query( $with_term_args );

if ( !$cat_query->have_posts() ) {

$cat_query = new WP_Query( $no_term_args );

}

//if there are posts setup the post and format below
while ( $cat_query->have_posts() ) {

$cat_query->the_post();
?>

<div class="post">

<?php the_post_thumbnail('thumbnail', array( 'class' => 'alignleft' , 'style' => 'border-top: none;' )); ?>

<?php echo ' View all items from <a href=" ' . get_category_link( $category->term_id )' '">' ?>

<div>

</div><!-- #post-## -->

<?php

} // while ( $cat_query->have_posts() )

wp_reset_postdata();

} // foreach($categories
?>


Sush D comments:

This solved it and helped me understand see how wp_query can be used pretty powerfully.
Thanks

2011-07-24

Romel Apuya answers:

How about trying this plugin
[[LINK href="http://wordpress.org/extend/plugins/custom-post-limits/"]]Custom Post Limits[[/LINK]]


Sush D comments:

This plugin doesn't appear to answer my question. I am able to limit the number of posts without a problem. Thanks

2011-07-24

Jerson Baguio answers:

Try this code i made some modification on your code.. It checks first if it has a posts with your custom field and if found display else proceed with the latest posts


<?php

//for each category, show one post

$cat_args=array(

'orderby' => 'name',

'order' => 'ASC'

);

$categories=get_categories($cat_args);

foreach($categories as $category)
{

// get the posts that has the custom taxonomy
// replace meta_key_name_here replace with you custom field and the meta_value_here for your custom value
$args = array('showposts'=>1, 'meta_key'=>'meta_key_name_here','meta_value'=>'meta_value_here');

//get the posts with the conditions in $args

$posts=get_posts($args);



//if there are posts setup the post and format below

if ($posts) {

foreach($posts as $post) {



setup_postdata($post);



?>



<div class="post">

<?php the_post_thumbnail('thumbnail', array( 'class' => 'alignleft' , 'style' => 'border-top: none;' )); ?>



<?php echo ' View all items from <a href=" ' . get_category_link( $category->term_id ).'">'?>



<div>



</div><!-- #post-## -->

<?php



} // foreach($posts



}else{ //else no post found with the custom taxonomy so query the latest post
$args=array('showposts' => 1,'category__in' => array($category->term_id),'caller_get_posts'=>1);

//get the posts with the conditions in $args
$posts=get_posts($args);
if ($posts) {

foreach($posts as $post) {



setup_postdata($post);



?>



<div class="post">

<?php the_post_thumbnail('thumbnail', array( 'class' => 'alignleft' , 'style' => 'border-top: none;' )); ?>



<?php echo ' View all items from <a href=" ' . get_category_link( $category->term_id ).'">'?>



<div>



</div><!-- #post-## -->

<?php



} // foreach($posts
}//if post
}// if ($posts

} // foreach($categories

?>