How to hide text if query has no posts? - wordpress

I want to hide the text with the word reviews if query has no posts. Please ask for suggestions.
is image > enter image description here
<h1 class="section-title"><span>Reviews</span></h1>
<?php
$terms = get_the_terms( $post->ID , 'movies', 'string');
$term_ids = wp_list_pluck($terms,'term_id');
$second_query = new WP_Query( array(
'post_type' => 'post',
'cat' => '21',
'tax_query' => array(
array(
'taxonomy' => 'movies',
'field' => 'id',
'terms' => $term_ids,
'operator'=> 'IN' //Or 'AND' or 'NOT IN'
)),
'posts_per_page' => 6,
'orderby' => 'date',
'post__not_in'=>array($post->ID)
) );
//Loop through posts and display...
if($second_query->have_posts()) {
while ($second_query->have_posts() ) : $second_query->the_post();
get_template_part( 'template-parts/content', 'list-01' );
?>
<?php endwhile; wp_reset_query(); }?>

No need of jQuery, just put the h1 tag after the If statement and before while. Then if your query is not empty it will display the title otherwise not
Like this:
<?php
$terms = get_the_terms( $post->ID , 'movies', 'string');
$term_ids = wp_list_pluck($terms,'term_id');
$second_query = new WP_Query( array(
'post_type' => 'post',
'cat' => '21',
'tax_query' => array(
array(
'taxonomy' => 'movies',
'field' => 'id',
'terms' => $term_ids,
'operator'=> 'IN' //Or 'AND' or 'NOT IN'
)),
'posts_per_page' => 6,
'orderby' => 'date',
'post__not_in'=>array($post->ID)
) );
//Loop through posts and display...
if($second_query->have_posts()) { ?>
<h1 class="section-title"><span>Reviews</span></h1>
<?php while ($second_query->have_posts() ) : $second_query->the_post();
get_template_part( 'template-parts/content', 'list-01' );
?>
<?php endwhile; wp_reset_query(); }?>

Related

How do i show only 1 post from latest 10 post?

my english language is not good. sorry!
this code is for checkbox in advanced custom field plugin.
i want show only 1 post(randomly) from latest 10 post.
please help me. thanks
('posts_per_page' => 10) and ('numberposts' => 10) is not working.
<?php
$gallery = array(
"offset" => "0",
'showposts' => '1',
'orderby' => 'rand',
'meta_query' => array(
array(
'key' => 'postcat',
'value' => '"selection"',
'compare' => 'LIKE'
)));
// query
$qgallery = new WP_Query( $gallery );
?>
<?php if( $qgallery->have_posts() ): ?>
<?php while( $qgallery->have_posts() ) : $qgallery->the_post(); ?>
<div class="fromgallery">
<a href="0" class="frgall">
<span class="frgdesc"><?php the_title() ?></span>
</a></div>
<?php endwhile; ?><?php endif; ?>
Try this
$gallery = array(
'post_type' => 'post',
'posts_per_page' => 10,
'order' => 'DESC',
'no_found_rows' => 'true',
'_shuffle_and_pick' => 1
'meta_query' => array(
array(
'key' => 'postcat',
'value' => '"selection"',
'compare' => 'LIKE'
)));
$qgallery = new \WP_Query( $gallery );
I only do change in your code. For randomly one post you need to use '_shuffle_and_pick' => 1 , 'posts_per_page' => 10 is from 10 post and 'order' => 'DESC' is for latest posts. for custom '_shuffle_and_pick' you need to add
add_filter( 'the_posts', function( $posts, \WP_Query $qgallery )
{
if( $pick = $qgallery->get( '_shuffle_and_pick' ) )
{
shuffle( $posts );
$posts = array_slice( $posts, 0, (int) $pick );
}
return $posts;
}, 10, 2 );
To show Random post , Please use below script:
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'no_found_rows' => 'true',
'_shuffle_and_pick' => 1
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'postcat',
'value' => '"selection"',
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query( $args );
//check is post found
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo get_the_title();
}
wp_reset_postdata();
} else {
echo 'no posts found';
}
I hope it will help you :)

WordPress: How can get all product of brand from specific a category with WooCommerce Plugin?

I am trying to print all products of a brand on from category page.
I find the wp_query but I can't print the current brand name of the brand to show all products in this page.
// get products
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'pwb-brand',
'field' => 'name',
'terms' => array ('NAME?????')
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'not found anyhting.' );
}
wp_reset_postdata();
For example, i need to get all product of brand name (XXX) that fount in category name (YYY)
This is answer after search :
$args = array(
'post_type' => array('post','product'),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'terms' => 82,
'field' => 'id'
),
array(
'taxonomy' => 'brand',
'terms' => 81,
'field' => 'id'
),
)
);
query_posts($args);
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
the_title();
}
}

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.

Show user post count while using "ultimate member" and "co-author"

I'm using the plugin ultimate member and co-author
and in the author page in the post tab
i'm trying to show the numbers of posts the user has written with this line
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'author',
'field' => 'slug',
'terms' => $user_login
)
),
);
$author_query = new WP_Query( $args );
if ( $author_query->have_posts() ) :
while ( $author_query->have_posts() ) : $author_query->the_post();
// Do your presentation
endwhile;
endif;
Would appreciate your help for the correct code
You will need a counter.
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'author',
'field' => 'slug',
'terms' => $user_login
)
),
);
$author_query = new WP_Query( $args );
$count=0;
if ( $author_query->have_posts() ) :
while ( $author_query->have_posts() ) : $author_query->the_post();
// Do your presentation
$count++;
endwhile;
endif;
echo $count;

How to get category listing of most popular products in woocommerce

How can I list out the top 5 most popular category(or category of most popular products) on my wordpress site home page.
I have used woocommerce plugin for products.
Thanks in advance for any suggestion or solution.
Since none of the answers is a solution to the author's question, here is what I came up with. This is a shortcode snippet that lists popular products by categories. By popular I mean most sold products ( as in total sales).
function bestselling_products_by_categories( $atts ){
global $woocommerce_loop;
extract(shortcode_atts(array(
'cats' => '',
'tax' => 'product_cat',
'per_cat' => '5',
'columns' => '5',
'include_children' => false,
'title' => 'Popular Products',
'link_text' => 'See all',
), $atts));
if(empty($cats)){
$terms = get_terms( 'product_cat', array('hide_empty' => true, 'fields' => 'ids'));
$cats = implode(',', $terms);
}
$cats = explode(',', $cats);
if( empty($cats) )
return '';
ob_start();
foreach($cats as $cat){
// get the product category
$term = get_term( $cat, $tax);
// setup query
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $per_cat,
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'tax_query' => array(
array(
'taxonomy' => $tax,
'field' => 'id',
'terms' => $cat,
'include_children' => $include_children,
)
),
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array( 'catalog', 'visible' ),
'compare' => 'IN'
)
)
);
// set woocommerce columns
$woocommerce_loop['columns'] = $columns;
// query database
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<?php if ( shortcode_exists('title') ) : ?>
<?php echo do_shortcode('[title text="'. $title .'" link="' . get_term_link( $cat, 'product_cat' ) . '" link_text="' . $link_text . '"]'); ?>
<?php else : ?>
<?php echo '<h2>'. $title .'</h2>'; ?>
<?php endif; ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
}
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
} add_shortcode( 'custom_bestselling_product_by_categories', 'bestselling_products_by_categories' );
You can use this by calling it as:
<?php echo do_shortcode('[custom_bestselling_product_by_categories cats="' . $term->term_id . '"]'); ?>
This shortcode has some options:
cats : the category ID or comma-separated IDs to retrieve the products from.
tax : the taxonomy to get the products from, default is product_cat
per_cat : number of products to retrieve
columns : number of columns to display
include_children : if false only direct children of the category will be displayed, if true then children of children will be displayed
title : title to display
link_text : the link text linked to the store
Notice that this snippet assumes you have a shortcode named title and it takes a few other parameters such as link and link_text arguments. You can always change this according to your theme.
Hope it helps.
I recommend you to check this page.
http://docs.woothemes.com/document/woocommerce-shortcodes/
array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'title',
'order' => 'asc',
'category' => ''
)
[product_category category="appliances"]
array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'title',
'order' => 'asc'
)
[top_rated_products per_page="12"]
Or you can use this plugin : https://wordpress.org/plugins/sp-woocommerce-best-selling-products-by-category/
Popular might be in many cases like most viewing, top selling. So i listed products by top selling. This way you can get top selling products and by this you can get category listing.
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '10',
'columns' => '4',
'fields' => 'ids',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'meta_query' => WC()->query->get_meta_query()
);
$best_sell_products_query = query_posts($query_args);
return $best_sell_products_query;

Resources