I have searched high and low and applied all of the solutions I've found here on Stackoverflow with no luck so trying a post to see if someone could assist me...
I am trying to display the posts that sit under a category in my custom post type taxonomy.
The CPT is called "Fabrics" and the taxonomy is called "Type" - Under "Type" I have a category called "Lycra"
I want to display all of the posts that exist under "Lycra"
Here is the code I have so far, but it just lists all of the posts under Fabrics:
<?php
$cat_terms = get_terms(
array(
'post_type' => 'fabrics',
'post_status' => 'publish',
'posts_per_page' => 9999999,
'orderby' => 'date',
'order' => 'DES',
'tax_query' => array(
array(
'taxonomy' => 'type',
'field' => 'slug',
'terms' => 'lycra',
),
),
)
);
if( $cat_terms ) :
echo '<ul class="fabric-listing">';
foreach( $cat_terms as $term ) :?>
<li class="each-fabric">
<a href="<?php echo get_term_link( $term );?>">
<img src="<?php the_field('imagecat', $term); ?>">
<div class="term-name">
<?php echo $term->name; ?>
</div>
</a>
</li>
<?php
wp_reset_postdata();
endforeach;
echo '</ul>';
endif;
?>
What am I missing here? Any help is super appreciated
Use get_posts and not get_terms
$cat_terms = get_posts(
array(
'post_type' => 'fabrics',
'post_status' => 'publish',
'numberposts' => -1,
'orderby' => 'date',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'type',
'field' => 'slug',
'terms' => 'lycra',
),
),
)
Related
I've written a wp_query that checks for featured products in woocommerce.
First it gathers all the categories and then it checks if they have featured products. It then displays the category name.
Only issue I have is it duplicates the cat name for each featured product inside it.
Any ideas what I've missed that causes the loop to act this way?
I think my code may be a bit bloated as well.
Thank you
<?php
$fpArgs = array(
'post_type' => 'product',
'tax_query' => array(
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
)
);
$featProds = new WP_Query( $fpArgs );
if ($featProds->have_posts()):
?>
<section class="standard featuredProducts">
<?php $cat_terms = get_terms('product_cat'); ?>
<ul class="nav nav-tabs" id="myTab" role="tablist">
<?php
foreach ( $cat_terms as $cat_term ):
$cat_query = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( $cat_term->slug ),
'operator' => 'IN'
),
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
)
)
) );
?>
<?php
if ( $cat_query->have_posts() ) :
while ( $cat_query->have_posts() ) :
$cat_query->the_post();
$catName = $cat_term->name;
?>
<li class="nav-item" role="presentation">
<a class="nav-link" id="cat-tab">
<?php echo $catName; ?>
</a>
</li>
<?php
endwhile;
endif;
endforeach;
?>
</ul>
</section>
<?php endif; ?>
I've worked out a solution...
I added all the categories into an array and used that to select only unique categories with an if statement.
Followed this solution which worked like a charm
https://wordpress.stackexchange.com/questions/154970/remove-duplicated-values-from-a-loop
I've got a custom post type that I've sorted by date from an ACF date picker field. I need to be able to filter these results based on a custom taxonomy that I've got. I can't seem to work out where to add the tax_query array. Keeps breaking the site.
Taxonomy name is 'pre_job_status', term to filter by is 'survey-complete'
Currently I have the following code
<?php
// get posts
$posts = get_posts(
'tax_query' => array(
array(
'taxonomy' => 'pre_job_status',
'field' => 'slug',
'terms' => array( 'survey-complete' )
),
),
array(
'post_type' => 'pre_jobs',
'posts_per_page' => -1,
'meta_key' => 'survey_date',
'orderby' => 'meta_value',
'order' => 'ASC',
));
if( $posts ): ?>
<hr>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<?php $requestor = get_field('pre_job_requestor', $client->ID ); ?>
<?php $survey_site = get_field('survey_site', $client->ID ); ?>
<li>
<?php the_field('survey_date'); ?> - <?php the_title(); ?> - <?php echo $requestor[0]->post_title; ?> - <?php echo $survey_site[0]->post_title; ?>
</li><hr>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Your get_posts() query should look like the following:
// get posts
$posts = get_posts(array(
'post_type' => 'pre_jobs',
'posts_per_page' => -1,
'meta_key' => 'survey_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'pre_job_status',
'field' => 'slug',
'terms' => array( 'survey-complete' )
),
),
));
The tax_query array should be added to the main query array.
I have a simple taxonomy-product_cat.php page, where I am trying to display only the products in the current category. Right now, it's displaying all products, not just the ones in the current category. Here is my code:
<ul class="products">
<?php
$current_cat_id = $wp_query->get_queried_object()->term_id;
$args = array(
'taxonomy' => 'product_cat',
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $current_cat_id,
'operator' => 'IN'
)
);
$products = new WP_Query( $args );
while ( $products->have_posts() ) : $products->the_post();
echo '<li><div class="product__preview"><img src="' . get_the_post_thumbnail_url() . '"></div><span>' . get_the_title() . '</span></li>';
endwhile;
wp_reset_query();
?>
</ul>
My client keeps changing the names/slugs of the category, so I need to get the products by category ID. Any idea why this is generating all products, instead of just the ones in the current category?
Update 2
The tax query need to have 2 arrays embedded in each other: 'tax_query' => array( array(
Removed the first unneeded 'taxonomy' => 'product_cat',
Replaced 'terms' => $current_cat_id, by 'terms' => array( get_queried_object()->term_id ),
Replaced wp_reset_query(); by wp_reset_postdata();
Your code will be:
$query = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( get_queried_object()->term_id ),
) )
) );
while ( $query->have_posts() ) : $query->the_post();
echo '<li><div class="product__preview"><img src="' . get_the_post_thumbnail_url() . '"></div><span>' . get_the_title() . '</span></li>';
endwhile;
wp_reset_postdata();
Tested and works
although its correct answer you can get it through category slug.
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'product_cat' => 'category slug here',
);
$products_all = new WP_Query( $args );
version tested 4.6.1
$terms = wp_get_post_terms( $product->id, 'product_cat' );
$args_listing = array(
'post_type' => 'product',
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $terms[0]->term_id,
) ),
'orderby'=> 'date',
'order'=> "desc",
'posts_per_page' => 10,
'paged' => $paged
);
I think you can try this. its working for me.
IN my wp site, I have 2 category and a few post like that..
cat_1- post 1, post 2, post 3.
cat_2- post 2, post 3, post 4.
When i am in post 3 page, i want to show releted article only from category 2.here is my code:but it returns empty. Probably i did not catch the logic. Any help would be highly appreciated.
<?php
$terms = get_the_terms( $post_id, 'category' );
if( empty( $terms ) ) $terms = array();
$term_list = wp_list_pluck( $terms, 'slug' );
$related_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish',
'post__not_in' => array( get_the_ID() ),
'orderby' => 'desc',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $term_list
),
array(
'taxonomy' => 'category',
'terms' => array('cat_1'),
'field' => 'slug',
'operator' => 'NOT IN',
),
),
);
$related = new WP_Query( $related_args );
if( $related->have_posts() ):
?>
<div class="post-navigation">
<h3>Related posts</h3>
<ul>
<?php while( $related->have_posts() ): $related->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
</div>
<?php
endif;
wp_reset_postdata();
?>
here is the following query ...may be helpful to you...
$custom_tex=array(array('taxonomy'=>'category','field'=>'slug','terms'=>'cat_2'));
$new = new WP_Query(array('post_type'=>'post','posts_per_page'=>-1, 'tax_query'=>$custom_tex,'order' => 'DESC','orderby' => 'ID' ));
while ($new->have_posts()) : $new->the_post();
echo $post->post_title;
echo "<br>";
endwhile;
I'm querying two taxonomies with this code:
$args = array(
'posts_per_page' => 1,
'offset' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true,
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => '16',
'field' => 'term_id',
),
array(
'taxonomy' => $this_taxonomy,
'terms' => $this_page,
'field' => 'slug',
)
)
);
$myposts2 = get_posts( $args );
And it returns a good result (the resulting post object has the right category and taxonomy/slug assignments).
But when I try to use subsequent functions like this:
<?php foreach ( $myposts2 as $post2 ) : ?>
<li class="product-details">
<?php get_the_post_thumbnail( $post2->ID, 'medium' ); ?><h3><?php $post2->post_title; ?></h3>
</li>
<?php
endforeach;
wp_reset_postdata();
?>
I end up with the wrong results for the permalink and no results for title or thumbnail.
Any ideas what I'm doing wrong?
Thank you, BA_Webimax! There were some additional things wrong with the code, but the reference link you sent helped me in the right direction. I wasn't expecting WP to require me to call the object $post (instead of $post2). Also, I was using the wrong functions to retrieve the bits. get_permalink() was fine, but I should have been using the_post_thumbnail() and the_title() instead of the functions I'd chosen.
You need to throw in a setup_postdata() call in there. See here for details... https://codex.wordpress.org/Function_Reference/setup_postdata
<?php foreach ( $myposts2 as $post2 ) :
setup_postdata($post2); ?>
<li class="product-details">
<?php get_the_post_thumbnail( $post2->ID, 'medium' ); ?><h3><?php $post2->post_title; ?></h3>
</li>
<?php
endforeach;
wp_reset_postdata();
?>