Ask your WordPress questions! Pay money and get answers fast! (more info)

help with my loop, show only cpt tag in loop

  • SOLVED

This loop works great. I am showing one custom post type for each "service" taxonomy. It also lists the taxonomy with the "post" below it. I want to show only a post with the tag "featured" under "service-tags". The loop below.

<?php

$terms = get_terms( 'service' );

foreach ( $terms as $term ) {

$loop = new WP_Query(
array(
'posts_per_page' => 1,
'tax_query' => array(
array( 'taxonomy' => $term->taxonomy, 'field' => 'slug', 'terms' => array( $term->slug ) )
)
)
);

if ( $loop->have_posts() ) : ?>
<div class="clearfix hentry">
<div class="span8 service-content">
<h3><?php echo $term->name; ?></h3>
<p><?php echo $term->description; ?></p>
</div>

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

<div class="pull-right highlight">
<?php get_the_image( array( 'custom_key' => array( 'pull-left' ), 'default_size' => 'thumbnail' ) ); ?>

<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
</div>


<?php endwhile; ?>
</div>
<?php endif; ?>


<?php } wp_reset_query(); ?>

Answers (4)

2013-05-21

Daniel Yoen answers:

Hello,

please, try this :

<?php

$terms = get_terms('service');

foreach ($terms as $term)
{
$loop = new WP_Query(array(
'posts_per_page' => 1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'service',
'field' => 'slug',
'terms' => array($term->slug)
),
array(
'taxonomy' => 'service-tags',
'field' => 'slug',
'terms' => array('featured'),
)
)));

if ($loop->have_posts()) : ?>

<div class="clearfix hentry">
<div class="span8 service-content">
<h3><?php echo $term->name; ?></h3>
<p><?php echo $term->description; ?></p>
</div>

<?php while ($loop->have_posts()) : $loop->the_post(); ?>

<div class="pull-right highlight">

<?php the_post_thumbnail('thumbnail'); ?>

<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>

</div>

<?php endwhile; ?>

</div>

<?php endif;
}
wp_reset_query();

?>


Rick Bible comments:

I found this solution last night, knew when I looked at it, you had it to. It works well

2013-05-21

Luis Abarca answers:

Try this, it get only 'featured' tag from service-tag taxonomy.


<?php

$terms = get_terms( 'service-tags' );

foreach ( $terms as $term ) {

// look for featured tag
if ( $term->slug != 'featured' ) {
continue;
}


$loop = new WP_Query(array(
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'slug',
'terms' => array( $term->slug )
)
))
);

if ( $loop->have_posts() ) : ?>
<div class="clearfix hentry">
<div class="span8 service-content">
<h3><?php echo $term->name; ?></h3>
<p><?php echo $term->description; ?></p>
</div>

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

<div class="pull-right highlight">
<?php get_the_image( array( 'custom_key' => array( 'pull-left' ), 'default_size' => 'thumbnail' ) ); ?>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
</div>
<?php endwhile; ?>

</div>
<?php endif; ?>


<?php
} // enforeach

wp_reset_query();
?>


Rick Bible comments:

no, i've done myself. Only the tagged item shows up (one) without the taxonomy term. Here is what I want:

BLUE CARS:
featured car {service tag)
RED CARS
featured car
BLUE CARS
featured car

Right now the loop lists the term "service" for each service and below each service it shows one custom post type. Thats whats so nice about this loop. I want to show the custom post type but only the most recent "featured".


Rick Bible comments:

Below is what I want to filter. it shows cpts. I want it to show cpt's with service-tag "featured".

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

<div class="pull-right highlight">
<?php get_the_image( array( 'custom_key' => array( 'pull-left' ), 'default_size' => 'thumbnail' ) ); ?>

<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
</div>


<?php endwhile; ?>

2013-05-21

Arnav Joy answers:

try this

<?php




while ( $loop->have_posts() ) : $loop->the_post();
$terms = wp_get_post_terms( get_the_ID(), 'service' );
$term_d = array();
if( !empty( $terms)){
foreach( $terms as $term )
$term_d[] = $term->slug ;
}

if( in_array( 'service-tags' , $term_d )) {
?>



<div class="pull-right highlight">

<?php get_the_image( array( 'custom_key' => array( 'pull-left' ), 'default_size' => 'thumbnail' ) ); ?>



<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>

</div>


<?php } ?>


<?php endwhile; ?>


Rick Bible comments:

no "posts" show up at all. Also, I am trying to get "featured" service-tag


Arnav Joy comments:

check this , see first two lines there you have to change name of the taxonomy and and name of the tag

<?php

$tax_name = 'service'; // change taxonomy name here
$tag_slug = 'featured'; // change tag slug here

while ( $loop->have_posts() ) : $loop->the_post();

$terms = wp_get_post_terms( get_the_ID(), $tax_name );

$term_d = array();

if( !empty( $terms)){

foreach( $terms as $term )

$term_d[] = $term->slug ;

}


if( in_array( $tag_slug , $term_d )) {

?>

<div class="pull-right highlight">


<?php get_the_image( array( 'custom_key' => array( 'pull-left' ), 'default_size' => 'thumbnail' ) ); ?>


<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
</div>



<?php } ?>


<?php endwhile; ?>



Rick Bible comments:

everything leaves the post out


Arnav Joy comments:

sorry not getting your last comment

2013-05-21

Hariprasad answers:

Hello,


Change your WP_query like this


$loop = new WP_Query(
array(
'posts_per_page' => 1,
'tax_query' => array(
array( 'taxonomy' => $term->taxonomy, 'field' => 'slug', 'terms' => 'featured' )
)
)
);


Rick Bible comments:

nothing shows up, "featured" is in service-tags taxonomy. It is not a "service" taxonomy. What I like about my loop is that it shows each service "category" under the custom post type. It then lists custom post types under each service. The featured is a tag under the same custom post type, "service-tags" with the tag of "featured"


Rick Bible comments:

Below is a loop I use to grab 4 "featured" in service tags. The challenge is to make this work within the loop I have, which grabs the newest custom post types just fine, but cannot filter like below.

<div id ="featured_portfolio_thumbs">
<?php $feature_query = new WP_Query( 'service-tags=featured&posts_per_page=4' ); ?>
<?php if( $feature_query->have_posts() ) : ?>

<?php while ( $feature_query->have_posts() ) : $feature_query->the_post(); ?>

<?php get_the_image( array( 'custom_key' => array( 'pull-right' ), 'default_size' => 'thumbnail' ) ); ?>


<?php endwhile; ?>
<?php endif; ?>
</div>
<?php wp_reset_postdata(); ?>


Hariprasad comments:

Hello,
Am little bit confused about featured tag. Can you please provide me the screenshot of a sample post that you want to display in your result(screenshot of add post in dashboard). if you can mark taxonomy and service tag in screenshot would be better.


Hariprasad comments:

Also provide the complete code that you used to get result..