Wordpress get_posts by category - wordpress

I have the following bit of code:
$args = array(
'posts_per_page' => -1,
'category' => 7,
'orderby' => 'name',
'order' => 'ASC',
'post_type' => 'product'
);
$posts = get_posts($args);var_dump($posts);
This should return one post I know that is in the category, but it isn't. If I leave out the 'category'-argument, I get all the products, so I know this should normally work. If I change the category to 1 and take out my custom post type (product), I get my default posts.
I can't see what's wrong with this. Can anyone spot what the problem is?

If you'd like you could accomplish the same thing but with WP_Query. This gets the post type "Product" with "posts_per_page" for the amount of posts, and "product_cat" for the product category. Hope this helps!
EDIT: If you'd like to do it your way maybe instead of "category" try "product_cat"
<ul class="products">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'shoes', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<h2>Shoes</h2>
<li class="product">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul><!--/.products-->

Related

WordPress - Display categories only if post count greater than / equal to 3

I am struggling with this code to display categories (with posts) only if posts in that category are greater than / equal to 3.
I am using WP_Query with args() to get the categories and display latest 3 posts from those categories.
Any help will be highly appreciated.
<?php
$categories = get_categories( $args );
foreach( $categories as $category ) {
$args = array(
'cat' => $category->term_id,
'posts_per_page' => 3,
'post_type' => 'post',
);
$terms = get_terms(array(
'taxonomy' => 'category'
) );
foreach($terms as $term) {
if($term->count >= 3) {
echo $term->name;
$the_query = new WP_Query( $args );
echo '<h3>' . $category->name . '</h3>'; // Display category name
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<h3>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
<h3>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
</a>
</h3>
<?php
endwhile;
}
}
}
wp_reset_postdata();
?>
The WP_Query will return posts for a specific category and not categories themself.
You need to grab all of the categories and loop through them and check the count.
https://developer.wordpress.org/reference/functions/get_terms/
It should be something like this: (untested!)
$terms = get_terms(array(
'taxonomy' => 'category'
) );
foreach($terms as $term) {
if($term->count >= 3) {
echo $term->name;
}
}
$categories = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
));
foreach ($categories as $category) {
$args = array(
'cat' => $category->term_id,
'posts_per_page' => -1,
'post_type' => 'post'
);
if ($category->count >= 3) {
echo $category->name;
$the_query = new WP_Query($args);
echo '<h3>' . $category->name . '</h3>'; // Display category name
while ($the_query->have_posts()) : $the_query->the_post();
?>
<h3>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
<h3>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
</a>
</h3>
<?php
endwhile;
}
}
wp_reset_postdata();
get_terms() array can take in a lot of arguments so please check the documentation first.
Let me know if you have any problems with it. I'll try to help :)

How can you loop through wordpress posts and using posts_per_page with no duplicates on the last page?

In wordpress i am creating a custom loop/query throughwhich i pass certain parameters. As i click through pages however the last page duplicates some posts/products inorder to satisfy the posts_per_page variable however i would like to specify that i dont want any repetitions. Is there a standard way to do this? It would seem like a pretty obvious point.
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => 'products', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="product">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li>
<?php endwhile; ?>
<?php previous_posts_link('« Previous', $loop ->max_num_pages);
next_posts_link(' Next »', $loop ->max_num_pages); ?>
<?php wp_reset_query();?>
It would seem like a pretty obvious point.
Not if you're using 'orderby' => 'rand' on your query, which is also very expensive by the way on large tables.
If you want to make sure that the items which already have been displayed will be excluded in the upcoming queries you'll need to save the post_ids which already has been displayed and pass them to the post__not_in parameter, see the codex page an search for post__not_in .
You could do something like this which should help you get the idea:
...
// don't forget to initialize the session somewhere
$already_displayed_post_ids = [];
if ( ! isset( $_SESSION['already_displayed_post_ids'] ) {
$_SESSION['already_displayed_post_ids'] = $already_displayed_post_ids;
} else {
$already_displayed_post_ids = array_merge( $already_displayed_post_ids, $_SESSION['already_displayed_post_ids'] );
}
$args = [
'post_type' => 'product',
'posts_per_page' => 5,
'product_cat' => 'products',
'orderby' => 'rand',
'post__not_in' => $already_displayed_post_ids
];
$loop = new WP_Query( $args );
...

Display only author posts in author page in Wordpress

What should I do to display only a certain author posts, in my author.php page? I don't mean an specific author (using author ID), but the default page for authors. With the current code, its displaying posts from all the other authors. Should I use a specific query?
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_args = array(
'post_type' => 'post',
// 'order' => 'DESC',
'posts_per_page' => 10,
'paged' => $paged
);
// custom query
$recent_posts = new WP_Query($custom_args);
// check that we have results
if($recent_posts->have_posts()) : ?>
<ul class="article_list">
<?php
// start loop
while ($recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="regular">
<a href="<?php echo get_permalink(); ?>">
<div class="text">
<p class="category"><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?></p>
<p class="autor"><?php the_author(); ?></p>
<h3 class="article_title"><?php echo mb_strimwidth(get_the_title(), 0, 80, '...'); ?></h3>
<p class="date"><?php echo get_the_date( 'Y-m-d' ); ?></p>
</div>
<div class="mask">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
echo '<div class="art_img" style="background: url('. $url.')"></div>';
?>
</div>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif;
echo '<div class="pagination">';
if(function_exists('wp_pagenavi')) { wp_pagenavi(array( 'query' => $recent_posts )); }
echo '</div>';
wp_reset_postdata();
?>
You can use get_posts function of wordpress.
Here is reference:
https://codex.wordpress.org/Template_Tags/get_posts
Pass the required authors Id in the argument list.
Hello just add global $current_user; above $paged variable on top of your provided snippet and add 'author' => $current_user->ID, in your array of $custom_args so it will display posts of specific user in author page
Add author__in into your query
Try this
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_args = array(
'post_type' => 'post',
'author__in'=> array(2,4,6), //Authors's id's you like to include
// 'order' => 'DESC',
'posts_per_page' => 10,
'paged' => $paged
);

Pagination With woocomerece without plugins

I have facing a problem with pagination in woocomerece.
I am using mystyle theme and with woocomerece plugin.
i want to display 12 product per page.my code is not working
Here is my code.
<?php
$per_page =12;
$numpost = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'product'"); //calculating the number of products
$totalpages= ceil($numpost/$per_page);//calculating the last page or total pages
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
//$page =(isset($_GET['page'])) ? (int)$_GET['page'] : 1 ;
$start=($page-1)* $per_page;
$limit="limit".($page-1)*$per_page.",$per_page";
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => $per_page, 'orderby' =>'date','order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="rcollproli">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
<?php woo_pagination(); ?>
</li><!-- /span3 -->
<?php endwhile; ?>
<?php
if($totalpages >=1){
for($x=1;$x<=$totalpages;$x++)
{
echo ''.$x.'';
}
}
?>
By default, WooCommerce uses the same "posts per page" setting that is used for blog posts.
But, you can filter it to be any value you like.
add_filter( 'loop_shop_per_page', 'so_27395967_products_per_page' );
function so_27395967_products_per_page(){
return 12;
}
I don't think you need a custom database query.
Here is the solution with code.
global $wpdb;
$per_page =12;
$numpost = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'product'"); //calculating the number of products
//$query= $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE post_type = 'product' orderby ='date'");
$totalpages= ceil($numpost/$per_page);
$page =(isset($_GET['page'])) ? (int)$_GET['page'] : 1 ;
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => $per_page,'paged' => get_query_var('paged'), 'orderby' =>'date','order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="rcollproli">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
<?php woo_pagination(); ?>
</li><!-- /span3 -->
<?php endwhile; ?>

How to display woocommerce product details based on category id?

Can anyone please help me how to display the woocommerce product details based on category id?
I know how to display product details based on category name. The code is,
<ul class="productshome">
<?php
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 2, 'product_cat' => 'Salwar-Kameez', 'orderby' =>'rand','order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="producthome">
<a>post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
In the above code 'product_cat' => 'Salwar-Kameez' will display the category name of salwar-kameez. But i need the product details should display based on category id
Changing 'product_cat' => 'Salwar-Kameez' to tag_ID' => 15 where 15 is the category id.
Also the code you proved above has some errors. You didn't close the <ul>, you didn't close the <li>, had a </div> at the end but no opening div and your link wouldn't work. I have fixed those:
<ul class="productshome">
<?php
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 55, 'tag_ID' => 15, 'orderby' =>'rand','order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="producthome">
<a href="<?php echo get_permalink($loop->post->ID) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>

Resources