Display posts based on CPT category Name - wordpress

I have post type named 'service'. It has two categories 'Left' & 'Right' I wish to display all posts of 'Left' category. Here's my code
<?php
$args = array (
'post_type' => 'service',
'post_status' => 'publish',
'order' => 'ASC',
'category_name' => 'Left',
'posts_per_page'=>-1
);
$posts = new WP_Query( $args );
if ( $posts->have_posts() ) {
while ( $posts->have_posts() ) {
$posts->the_post();
//$image11 = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
?>
<li><i class="fa fa-file-text"></i> <?php the_title(); ?></li>
<?php } } wp_reset_postdata(); ?>
The above code returns nothing.

Please try with below:
On the $args Array please use the Left category ID insted of category_ID on "category" column.
<?php
$args = array (
'post_type' => 'service',
'post_status' => 'publish',
'order' => 'ASC',
'category' => 'category_ID',
'posts_per_page' =>-1
);
$posts = new WP_Query( $args );
if ( $posts->have_posts() ) {
while ( $posts->have_posts() ) {
$posts->the_post();
//$image11 = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
?>
<li>
<a href="<?php the_permalink(); ?>">
<i class="fa fa-file-text"></i>
<?php the_title(); ?>
</a>
</li>
<?php
}
}
wp_reset_postdata();
?>

Try this with the category name
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category', //name of texonomy
'field' => 'slug',
'terms' => 'Left'
)
)
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
//your code
endwhile;
endif;

Related

How can I sort best selling products in woocommerce

I want sort best selling products in shop page
how can I do this?
Use total_sales for 'meta_key' and meta_value_num for 'orderby'
<?php
$args = array(
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'posts_per_page' => 10,
);
$product_query = new WP_Query( $args );
while ( $product_query ->have_posts() ) : $product_query ->the_post();
?>
<h3><?php the_title(); ?></h3>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
For specific category or sub-category
<?php
$args = array(
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => array($categories_id)
),
)
);
$product_query = new WP_Query( $args );
while ( $product_query ->have_posts() ) : $product_query ->the_post();
?>
<h3><?php the_title(); ?></h3>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
For woo-commerce default query
add_filter('woocommerce_get_catalog_ordering_args','woocommerce_catalog_orderby' );
function woocommerce_catalog_orderby( $args ) {
$args['meta_key'] = 'total_sales';
$args['orderby'] = 'meta_value_num';
return $args;
}

How can I get only those categories which has products on sale?

I am trying to query woocommerce product categories but only those which have on sale products. Is there any possibilities? The result is hierarchy parent > child. I want to show both parent and its child. i.e. if child has product on sale print the parent category as well.
Here is the code I wrote so far
<ul class="accordion list-group sub-catalog">
<?php $terms = get_terms('product_cat', array( 'parent' => 0, 'exclude' => '15' ));
if( $terms ):
$original_query = $wp_query;
foreach ( $terms as $key => $term ):
$child = get_terms(
'product_cat',
array(
'child_of' => $term->term_id,
'hide_empty' => true
)
);
?>
<li class="accordion-card list-group-item">
<div class="acc-card-title">
<?php echo $term->name; ?>
<?php if ( ! $child ){ ?>
<?php
} else {
?>
<span class="fa fa-plus"></span>
<?php
}
?>
</div>
<ul class="accordion list-group sub-catalog">
<?php
$child_terms = get_terms(
'product_cat',
array(
'child_of' => $term->term_id,
'hide_empty' => true
)
);
foreach ( $child_terms as $child_term ) {
$re_child_terms = get_terms(
'product_cat',
array(
'child_of' => $child_term->term_id,
'hide_empty' => true
)
);
if ( ! $re_child_terms ){
?>
<li class="accordion-card list-group-item">
<div class="acc-card-title">
<?php echo $child_term->name; ?>
</div>
</li>
<?php
}
}
?>
</ul>
</li>
<?php
endforeach;
$wp_query = null;
$wp_query = $original_query;
?>
</ul>
<?php endif; ?>
Thanks in advance.
<ul class="accordion list-group sub-catalog">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'rand',
'meta_query' => array(
'relation' => 'OR',
array( // Simple products type
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array( // Variable products type
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
$terms = [];
while ( $loop->have_posts() ) : $loop->the_post();
$terms = array_diff($terms,wp_get_post_terms( get_the_id(), 'product_cat',['fields'=>'ids'] ) );
$term = reset($terms);
?>
<li class="accordion-card list-group-item">
<div class="acc-card-title">
<?php echo $term->name; ?>
</div>
</li>
<?php
endwhile;
} else {
echo __( '' );
}
wp_reset_postdata();
?> </ul>
this is how I get the on sale products and get their category name . where to use array_diff function
The code below solved the problem.
<ul class="accordion list-group sub-catalog">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'rand',
'meta_query' => array(
'relation' => 'OR',
array( // Simple products type
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array( // Variable products type
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
$alreadyDisplayed = [];
while ( $loop->have_posts() ) : $loop->the_post();
$term_list = wp_get_post_terms($post->ID, 'product_cat', array("all"));
foreach($term_list as $term_single) {
if ( !in_array( $term_single->name, $alreadyDisplayed) ) {
echo ' <li class="accordion-card list-group-item">
<div class="acc-card-title"><a href="'.get_term_link($term_single).'" class="salecats">';
echo $term_single->name;
echo '</a><br></div>
</li>';
$alreadyDisplayed[] = $term_single->name;
}
}
?>
<?php
endwhile;
} else {
echo __( '' );
}
wp_reset_postdata();
?> </ul>

get custom woocommerce product

i want to get 10 product from a category in woocommerce
for example, for get latest post of a posts category i use the following code
<?php $posts = get_posts( 'category=17&numberposts=5' ); ?>
<?php if( $posts ) : ?>
<ul>
<?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
<li><i class="circle"></i><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
i want a code, like this for get woocommerce products
try this example :
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => 26,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
)
)
);
$products = new WP_Query($args);
/* your loop */
Hope this will helps you.
Try this example,
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'product_cat' => 'hoodies'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
endwhile;
wp_reset_query();
?>
OR
<?php
$args = array( 'post_type' => 'product', 'category' => 34, 'posts_per_page' => -1 );
$products = get_posts( $args );
?>
Hope this will helps you.
For more details please visit,
Woocommerce get products
why not use woocommerce's product_category shortcode?
<?php echo do_shortcode("[product_category category='17' limit='5']"); ?>
It will list the products same as in shop page. With these you are safe with product attributes like if when it's out of stock.

Exclude Draft Post in related Post Wordpress

I am currently using a custom code for related post filtered by category that displays 4 related posts.
My code works fine except it also displays the draft posts. which ideally it should and is a bit frustrating. Here is my code for the related post.
<div class="relatedposts">
<?php
// get current post categories and tags
$categories = get_the_category($post->ID);
$tags = get_the_tags($post->ID);
if ($categories || $tags) {
$category_ids = array();
if($categories)
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$tag_ids = array();
if($tags)
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $category_ids
),
array(
'taxonomy' => 'post_tag',
'field' => 'id',
'terms' => $tag_ids
)
),
'post__not_in' => array($post->ID),
'posts_per_page'=> 4, // Number of related posts that will be shown.
);
// query posts
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
?>
<div class="related-post-title">
<?php
echo "<h2>Related Health Hub Articles</h2>";
?>
</div>
<?php
while( $my_query->have_posts() ) { $my_query->the_post();
// display each post
?>
<div class="related-post-thumb col-sm-3">
<a href='<?php the_permalink(); ?>' rel='canonical' class="related-wrapper">
<div class="related-thumb"><?php the_post_thumbnail(array(150,100)); ?></div>
<h4 class="related-title"><?php the_title();?></h4>
</a>
</div>
<?php
}
}
}
wp_reset_postdata();
?>
</div>
I have reviewed your code. you have missed the post_status column. Anything with the status "publish" is what you want.
Please add 'post_status' => 'publish' in $args array.
You can see built-in WordPress functions.
http://codex.wordpress.org/Integrating_WordPress_with_Your_Website
Try this code
<div class="relatedposts">
<?php
// get current post categories and tags
$categories = get_the_category($post->ID);
$tags = get_the_tags($post->ID);
if ($categories || $tags) {
$category_ids = array();
if($categories)
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$tag_ids = array();
if($tags)
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $category_ids
),
array(
'taxonomy' => 'post_tag',
'field' => 'id',
'terms' => $tag_ids
)
),
'post__not_in' => array($post->ID),
'posts_per_page'=> 4, // Number of related posts that will be shown.
'post_status' => 'publish'
);
// query posts
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
?>
<div class="related-post-title">
<?php
echo "<h2>Related Health Hub Articles</h2>";
?>
</div>
<?php
while( $my_query->have_posts() ) { $my_query->the_post();
// display each post
?>
<div class="related-post-thumb col-sm-3">
<a href='<?php the_permalink(); ?>' rel='canonical' class="related-wrapper">
<div class="related-thumb"><?php the_post_thumbnail(array(150,100)); ?></div>
<h4 class="related-title"><?php the_title();?></h4>
</a>
</div>
<?php
}
}
}
wp_reset_postdata();
?>
</div>
$search_query=get_search_query();
function __extra_where($sql){
global $wpdb;
return ' AND '.$wpdb->prefix.'posts.post_status="publish" AND ( 1=1 '.$sql.' ) ';
}
add_filter( 'posts_where', '__extra_where', 10, 2 );
$query = new WP_Query(array(
'post_status' => array( 'publish' ),
'post_type' => ['post'/*your post_type here */],
's'=>$search_query,
'order' => 'ASC'
));
remove_filter( 'posts_where', '__extra_where', 10 );

How to list all category from custom post type?

I have a post type called 'dining' and has a taxonomy called 'dining-category'.
What I want to do is, I want to display all the category from post type 'dining' in my footer area.
In WordPress 4.6 get_terms is deprecated. So there is an alternate of this (get_categories) Read this
And here is Example code:
<?php
$args = array(
'taxonomy' => 'dining-category',
'orderby' => 'name',
'order' => 'ASC'
);
$cats = get_categories($args);
foreach($cats as $cat) {
?>
<a href="<?php echo get_category_link( $cat->term_id ) ?>">
<?php echo $cat->name; ?>
</a>
<?php
}
?>
Hope this will help you.
<?php
$args = array(
'type' => 'dining',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'taxonomy' => 'dining-category',
'pad_counts' => false );
$categories = get_categories($args);
echo '<ul>';
foreach ($categories as $category) {
$url = get_term_link($category);?>
<li><?php echo $category->name; ?></li>
<?php
}
echo '</ul>';
?>
If category not assigned any post it will not show. therefore assign any post. This code running perfectly.
<?php
$wcatTerms = get_terms(
'category', array('hide_empty' => 0, 'number' => 3, 'order' =>'asc', 'parent' =>0));
foreach($wcatTerms as $wcatTerm) :
?>
<small><?php echo $wcatTerm->name; ?></small>
<?php
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $wcatTerm->slug,
)
),
'posts_per_page' => 1
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$imgurl = get_the_post_thumbnail_url( get_the_ID(), 'full' );
$title=get_the_title($post->ID);
?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php endwhile; wp_reset_postdata(); ?>
<?php endforeach; ?>
use get_terms() function to fetch custom taxonomy by its slug, in your case slug is dining-category.
read function refrence from wordpress codex website and try this.

Resources