fetch products if image is found in woocommerce? - woocommerce

i want to show image only products in woocommerce.
My fetch query is given below
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 9, 'orderby' =>'date','orderby' => 'rand' );
$loop = new WP_Query( $args );
how can i get the image products only?

Are you looking for something like this ?
<?php
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 9, 'orderby' =>'date','orderby' => 'rand' );
$loop = new WP_Query( $args );
while ($loop->have_posts()) : $loop->the_post();
echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
endwhile;
?>
UPDATED
$args = array(
'post_type' => 'product',
'stock' => 1,
'posts_per_page' => 9,
'orderby' =>'date',
'orderby' => 'rand',
'meta_query'=>array(
array(
'key'=>'_thumbnail_id',
'compare' => 'EXISTS'
)
)
);
Change arg to get product if it has feature image.

Related

How to get category wise post in my custom template page

Here my is code. I want display related post in my custom page template. Now it will work fine only admin side. But when I was not logged in my account and also if logged in as a subscriber at that time it will not work perfectly. Here is my code please help me.
$args = array('post_type' => 'psdupload',
'posts_per_page' => 4,
'category__in' => wp_get_post_categories(get_the_ID()),
'post__not_in' => array (get_the_ID()),
'post_status' => 'publish',
);
$loop = new WP_Query($args);
//FOR CHECK IF POST ARRAY NULL OR NOT
if($loop->have_posts())
{
$loop = new WP_Query($args);
}
else
{
$recent_args = array(
'post_type' => 'psdupload',
'posts_per_page' => 4,
'orderby' => 'date',
'order' => 'DESC',
'post__not_in' => array (get_the_ID()),
'post_status' => 'publish',
);
$loop = new WP_Query( $recent_args );
}
further, I will use "$loop" variable as per my requirement.
You can add follows code snippets -
global $post;
$args = array(
'post_type' => 'psdupload',
'posts_per_page'=> 4,
'category__in' => wp_get_post_categories($post->ID),
'post__not_in' => array($post->ID),
'post_status' => 'publish',
);
$loop = new WP_Query($args);
if( !$loop->have_posts() ) {
$args = array(
'post_type' => 'psdupload',
'posts_per_page' => 4,
'orderby' => 'date',
'order' => 'DESC',
'post__not_in' => array($post->ID),
'post_status' => 'publish',
);
$loop = new WP_Query($args);
}
if( $loop->have_posts() ) {
?>
<ul>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<?php the_title(); ?>
</li>
<?php endwhile;
wp_reset_postdata(); ?>
</ul>
<?php
}

Prioritizing wp_query by meta key

I have two custom fields for views. weekly_views and all_views. The weekly views custom field is deleted every week and starts counting views again from 0. So now what I want to achieve is show 12 posts by weekly views but when the custom field is deleted and unless there are views on those posts the query shows nothing. I want to show here posts by all_views instead of no posts.
My query goes as follows but it's not working as I want. In short what I want to achieve is to show posts by weekly_views custom field but if there's no post then show posts by all_views. And also if there's less than 12 posts by weekly_views then show weekly_views posts first and then remaining posts by all_views.
$args = array(
'post_type' => array( 'custom_post_type_1', 'custom_post_type_2'),
'posts_per_page' => '12',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'weekly_views',
),
array(
'key' => 'all_views',
),
),
);
The above code is returning me posts but are sorted by all_views.
Edit
The new query that's working for me
<?php
$args = array(
'post_type'=> array( 'custom_post_type1', 'custom_post_type2'),
'posts_per_page' => '12',
'meta_key' => 'weekly_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$the_query = new WP_Query( $args );
if ($the_query->post_count < 12) {
$countweeklyposts = $the_query->post_count;
$showallpostscount = 12 - $countweeklyposts;
$args2 = array(
'post_type'=> array( 'band', 'artist'),
'posts_per_page' => $showallpostscount,
'meta_key' => 'all_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$the_query2 = new WP_Query( $args2 );
}
?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
//Code to show posts goes here
<?php
endwhile;
wp_reset_postdata();
?>
<?php while ($the_query2 -> have_posts()) : $the_query2 -> the_post(); ?>
//Code to show posts goes here
<?php
endwhile;
wp_reset_postdata();
?>
You could do this too if you want a little less code
<?php
$args = array(
'post_type'=> array( 'custom_post_type1', 'custom_post_type2'),
'posts_per_page' => '12',
'meta_key' => 'weekly_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$args2 = array(
'post_type'=> array( 'band', 'artist'),
'posts_per_page' => '12',
'meta_key' => 'all_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
if ($query->post_count > 12) {
$query_args = $args;
}else if($query->post_count < 12){
$query_args = $args2;
}
$query = new WP_Query( $query_args );
while ($query -> have_posts()) : $query -> the_post();
//Code to show posts goes here
endwhile;
wp_reset_postdata();
?>

Wordpress get Custom Post by Custom Taxonomy

I created a custom post and Custom Taxonomy
I need to search post using taxonomy
used this code but result is null
$properties = new WP_Query(
array( 'posts_per_page' => 10,
'orderby' => 'ID',
'order' => 'DESC',
'post_type' => 'real-property',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'tenure',
'terms' => 'val',
'field' => 'slug'
)
),
)
);
I found a solution myself: issue is WP_Query() function passed valued in array
<?php
$args = array( 'posts_per_page' => 10,
'orderby' => 'ID',
'order' => 'DESC',
'post_type' => 'real-property',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'tenure',
'terms' => 'val',
'field' => 'slug'
)
),
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
wp_reset_postdata();
else :
esc_html_e( 'Sorry, no posts matched.' );
endif;
?>

Wordpress wp_query error in marketpress

I am using the following function to get markerpress products in the category 'featured' and display the product images in a slideshow.
$args = array(
'category_name' => 'featured',
'post_type' => 'product'
);
$wp_query1 = new WP_Query( $args );
if (have_posts()) : while ( $wp_query1->have_posts() ) : $wp_query1->the_post()
Problem is that it does not fetch any data. I am forced to just use the below function which displays all the data.
$wp_query1 = new WP_Query('post_type=product');
What am I doing wrong and how can I set limits and also sort the data. Thanks.
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category_name' => 'featured',
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'product',
'post_status' => 'publish'
);
$posts_array = get_posts( $args ); ?>

Adding Pagination to Wordpress query for categories

I am trying to add pagination to a custom category list I have for a website gallery using WordPress. I have tried to follow a few guides but unable to add the paging function to my query. I have added my current queries.
Any help would be appreciated.
<?php
global $paged;
$curpage = $page ? $paged :1;
$args2 = array(
'taxonomy' => 'media-category',
//'parent' => '5',
'child_of' => 7,
'orderby' => 'date',
'order' => 'ASC',
'hide_empty' => 0,
'number' => 99,
'posts_per_page' => 3,
'paged' => $paged,
);
?>
<ul class="gallery-list small-block-grid-1 medium-block-grid-2 large-block-grid-3">
<?php
$categories = get_categories($args2);
foreach ($categories as $category) {
$args = array(
'post_type' => 'attachment',
'post_status' => 'any',
'posts_per_archive_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'media-category',
'field' => 'slug',
'terms' => array($category->name, 'featured'),
'operator' => 'AND',
),
),
);
$custom_query = new WP_Query($args);
?>
<?php while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
I found the answer.
// Sets manual pagination
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$posts_per_page = 12;
$offset = ($posts_per_page * $paged) - 12 ;
$args = array(
'orderby' => 'id',
'order' => 'DESC',
'hide_empty' => 0,
'number' => $posts_per_page,
'offset' => $offset,
);
$categories = get_terms($taxonomies, $args);
This sets the page up to have 12 items per page and offsets the page number.

Resources