I have the loop below that is doing the following:
Looping through all my custom taxonomy terms and seeing if the
checkbox "category on homepage" is selected, it shuffles those
selected.
It then loops through those terms that are selected and pulls out 1 custom post at random for each of those terms.
It then outputs a box with the term name and an image from the post selected.
The problem I'm having is that posts are tagged with multiple terms, so chances are it will select the same post across the different terms.
How can I ensure that the posts that are selected are unique?
<?php $terms = get_terms('project-categories', array(
'orderby' => 'rand',
'hide_empty' => 1,
)
);
shuffle($terms);
foreach ( $terms as $term ) {
if(get_field('category_on_homepage', 'project-categories_' . $term->term_id)) {
if (in_array('Yes', get_field('category_on_homepage', 'project-categories_' . $term->term_id))) {
$termid = $term->term_id;
$termname = $term->name;
$termslug = $term->slug;
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'post_type' => 'projects',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'project-categories',
'field' => 'id',
'terms' => $termid
)
)
);
$projects = get_posts( $args );
foreach ( $projects as $post ) {
setup_postdata($post); ?>
<a class="service" href="<?php echo $termslug; ?>">
<span class="service-image" style="background-image:url('<?php echo the_field('thumb_image'); ?>');"></span>
<span class="service-title"><?php echo $termname; ?></span>
</a>
<?php }
wp_reset_postdata();
}
}
} ?>
Build an array of used post ids, then use the post__not_in query parameter to exclude the used posts from future queries.
<?php
$terms = get_terms('project-categories', array(
'orderby' => 'rand',
'hide_empty' => 1
)
);
$used_posts = array();
shuffle($terms);
foreach ( $terms as $term ) {
if(get_field('category_on_homepage', 'project-categories_' . $term->term_id)) {
if (in_array('Yes', get_field('category_on_homepage', 'project-categories_' . $term->term_id))) {
$termid = $term->term_id;
$termname = $term->name;
$termslug = $term->slug;
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'post_type' => 'projects',
'post_status' => 'publish',
'post__not_in' => $used_posts,
'tax_query' => array(
array(
'taxonomy' => 'project-categories',
'field' => 'id',
'terms' => $termid
)
)
);
$projects = get_posts( $args );
foreach ( $projects as $post ) {
setup_postdata($post);
array_push($used_posts, $post->ID);
?>
<a class="service" href="<?php echo $termslug; ?>">
<span class="service-image" style="background-image:url('<?php echo the_field('thumb_image'); ?>');"></span>
<span class="service-title"><?php echo $termname; ?></span>
</a>
<?php
}
wp_reset_postdata();
}
}
}
?>
Related
I have a product category called Cable, and I want to grab all posts assigned to this category and output the title of said posts. This category is from the custom taxonomy 'Category' that woo-commerce adds, I'm not sure if that makes things harder? but I haven't found a solution yet.
Could anyone assist with this?
Try this code:
And modify as per your requirements:
add_shortcode( 'specific_category_posts', 'kb_get_posts' );
function kb_get_posts() {
$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( '18' ), //Your custom category id
),
),
)
);
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<h2>' . get_the_title() . '</h2>';
}
}
wp_reset_postdata();
}
Use this code to show all posts assigned to the category cable
<?php
global $post;
$args = array( 'numberposts' => 10, 'category_name' => 'cable' );
$posts = get_posts( $args );
foreach( $posts as $post ): setup_postdata($post);
?>
<div>
<?php
the_title();
the_excerpt(); ?>
</div>
<?php endforeach; ?>
I have created with list
add_shortcode( 'pi_cable_posts', 'pi_posts' );
function pi_posts() {
$pi_query = new WP_Query(
array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1, //--1 for unlimited and number to limit
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( 'YOUR ID HERE' ), //ADD YOUR ID HERE
),
),
));
if ( $pi_query ->have_posts() ) {
echo '<ul>';
while ( $pi_query ->have_posts() ) {
$pi_query ->the_post();
echo '<li><h2><a href="'.get_the_permalink().'">' . get_the_title() . '</h2></li>';
}
echo '</ul>';
}
wp_reset_postdata();}
You can use WP_Query() with tax_query().
EDIT:
Try out this code to get products on category page.
add_shortcode('product_list', 'zillion_show_products_title_by_cat', 10);
function zillion_show_products_title_by_cat()
{
if(!is_product_category()){
return;
}
$term = get_queried_object();
ob_start();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $term->term_id, // 21 is category id When you have more term_id's seperate them by comma.
'operator' => 'IN'
)
);
$the_query = new WP_Query($args);
// The Loop
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
echo '<h3>' . get_the_title() . '</h3>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
$contents = ob_get_clean();
return $contents;
}
I have made this code and the variable $link is not returning the product url, instead is returning home url. Please some Help! What I want is to get the product link selecting it by category. The display is like a menu by category but with the link going directly to one product I selected by category.
<ul>
<?php
$categories= get_terms( 'product_cat', array( 'parent' => 15, 'hide_empty' => false ) );
foreach($categories as $category): ?>
<li>
<div class="grid-subheader">
<?php
$metaArray = get_option('taxonomy_' . $category->term_id);
$productCatMetaTitle = $metaArray['wh_meta_title'];
$type = get_term_by( 'id', $category->parent, 'product_cat' );
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $category->term_id,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
)
)
);
$products = new WP_Query($args);
foreach ($products as $prod){
$link = get_the_permalink($prod->ID);
}
?>
<h3><?php echo $productCatMetaTitle; ?></h3>
<p><?php echo $category->description; ?></p>
<a href="<?php echo $link; ?>">
<div class="button-menu">
<?php //echo $type->name; ?> <?php echo $category->name; ?>
</div>
</a>
</div>
</li>
<?php endforeach; ?>
</ul>
Try the following revisited code:
<ul>
<?php
$taxonomy = 'product_cat';
$category_terms = get_terms( $taxonomy, array( 'parent' => 15, 'hide_empty' => false ) );
foreach( $category_terms as $term ) { ?>
<li>
<div class="grid-subheader">
<?php
$meta = get_option('taxonomy_' . $term->term_id);
$title = isset($meta['wh_meta_title']) ? $meta['wh_meta_title'] : '';
$type = get_term_by( 'id', $term->parent, $taxonomy );
$query = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => 1,
'fields' => 'ids',
'tax_query' => array( array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => array( $term->term_id ),
) ),
) );
$product_id = reset($query);
?>
<h3><?php echo $title; ?></h3>
<?php if ( ! empty($term->description) ) ?>
<p><?php echo $term->description; ?></p>
<a href="<?php echo get_permalink($product_id); ?>">
<div class="button-menu"><?php echo $term->name; ?></div>
</a>
</div>
</li>
<?php
}
?>
</ul>
I am developing a woocommerce website, and i want to show product list according to a category on the product-category page i.e: www.wesiteName.com/product-category/ladies. I want to show all the ladies products on this link but how? My code didn't work.
I am using this function woocommerce_page_title() to get category name but it print the category name but didn't pass name in wp query don't know why.
if (is_product_category()) :
$title = woocommerce_page_title();
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'product_cat' => $title,
'orderby' => 'post_title',
'order' => 'DESC',
);
$loop = new WP_Query( $args );
endif;
It shows all the products from all categories. I just want to show a particular product list according to a category.
Try this code.
$args = array(
'number' => $number,
'orderby' => 'title',
'order' => 'ASC',
'hide_empty' => $hide_empty,
'include' => $ids
);
$product_categories = get_terms( 'product_cat', $args );
$count = count($product_categories);
if ( $count > 0 ){
foreach ( $product_categories as $product_category ) {
echo '<h4><a href="' . get_term_link( $product_category ) . '">' .
$product_category->name . '</a></h4>';
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
// 'terms' => 'white-wines'
'terms' => $product_category->slug
)
),
'post_type' => 'product',
'orderby' => 'title,'
);
$products = new WP_Query( $args );
echo "<ul>";
while ( $products->have_posts() ) {
$products->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php
}
echo "</ul>";
}
}
Hope its working for you
I'm trying to display a post type’s categories and output the posts within that category, but I have another taxonomy that I want to exclude. This code is showing all Taxonomies and Terms registered to the specific post type.
The second taxonomy that I am trying to exclude is named "manufacturer" and is registered to multiple post types. I have tried doing 'operator'=>'NOT EXISTS' as well as what you see below, with the if...continue but nothing seems to work !!
<?php
$post_type = 'equipment';
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
$terms = get_terms( $taxonomy );
foreach( $terms as $term ):
if ( $term->slug == 'manufacturer' )
continue;
?>
<div class="row">
<div class="col-xs-12">
<h2><?php echo $term->name; ?></h2>
</div>
<div class="col-xs-12">
<?php
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$posts = new WP_Query($args);
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<h4><?php echo get_the_title(); ?></h4>
<?php endwhile; endif; ?>
</div>
</div>
<?php endforeach;
endforeach; ?>
)
$args = array(
'post_type' => array('equipment'),
'posts_per_page' => -1,
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'manufacturer',
'field' => 'slug',
'operator' => 'NOT IN',
),
),
);
$query = new WP_Query( $args );
I'm trying to overwrite the default 'Blog pages show at most' which is set to 5 posts. I have a custom post type called 'FAQs', which has the argument 'posts_per_page' => 999 in the query for getting all the posts of this type, however I can't seem to override the default restriction in the WordPress setting. My code for the FAQ query is below, which works on my local machine (MAMP) but not when I upload it to live. how do I show all posts of that type?
<?php
wp_reset_query();
// Query for getting custom taxonomy 'FAQ Type' of custom post type 'FAQs'
$cat_args = array (
'taxonomy' => 'faq_type',
'exclude' => array(12),
'posts_per_page' => 999,
//'show_all' => true,
'orderby' => 'simple_page_ordering_is_sortable'
);
$categories = get_categories ( $cat_args );
foreach ( $categories as $category ) {
//wp_reset_query();
$cat_query = null;
// Query for getting posts of custom post type 'FAQs'
$args = array (
'post_type' => 'faq',
'faq_type' => $category->slug,
'posts_per_page' => 999,
//'show_all' => true,
'orderby' => 'simple_page_ordering_is_sortable',
);
$cat_query = new WP_Query( $args );
if ( $cat_query->have_posts() ) { ?>
<?php echo "<h2>". $category->name ."</h2>"; ?>
<ul id="resident-accordion" class="accordion white-bg-accordion" data-accordion data-allow-all-closed="true" role="tablist">
<?php
while ( $cat_query->have_posts() ) {
$cat_query->the_post();
?>
<li class="accordion-item faq-content <?php //if ($firstLoop === true) { echo "is-active"; }?>" data-accordion-item>
<?php the_title(); ?>
<div class="accordion-content" data-tab-content>
<?php the_content(); ?>
</div>
</li>
<?php
} //wp_reset_query();
wp_reset_postdata(); //End WHILE
echo "</ul>";
} //End IF
wp_reset_postdata();
//wp_reset_query();
} //End FOR
?>
You can try to use this code below :
<?php
$cat_args = array(
'taxonomy' => 'faq_type',
'exclude' => array(7),
'orderby' => 'simple_page_ordering_is_sortable'
);
$categories = get_terms( $cat_args );
foreach ( $categories as $category )
{
$args = array(
'post_type' => 'faq',
'posts_per_page' => -1, // load all posts
'orderby' => 'simple_page_ordering_is_sortable',
'tax_query' => array(
array(
'taxonomy' => 'faq_type',
'field' => 'slug',
'terms' => $category->slug
)
)
);
$cat_query = new WP_Query( $args );
// enter the rest of your code below
}
Or you can use get_posts() to receive posts list.
<?php
$cat_args = array(
'taxonomy' => 'faq_type',
'exclude' => array(7),
'orderby' => 'simple_page_ordering_is_sortable'
);
$categories = get_terms( $cat_args );
foreach ( $categories as $category )
{
$posts = get_posts(array(
'numberposts' => -1, // get all posts.
'tax_query' => array(
array(
'taxonomy' => 'faq_type',
'field' => 'slug',
'terms' => $category->slug,
'operator' => 'IN',
),
),
'post_type' => 'faq',
));
// enter the rest of your code below
}
Cheers!
You found your answer by now I hope, but you were so close.
Instead of:
'posts_per_page' => 999,
try:
'posts_per_page' => -1,
see: Pagination Parameters