How to fetch the woocommerce product image from wp_query - php

Hi I am inflating the dropdown the woocommerce product titles with the below code
<select Name='choose' id="chooseme">
<?php
$args = array( 'post_type' => array('product') ,'posts_per_page' => 100);
$loop = new WP_Query( $args );
;
while ( $loop->have_posts() ) :
$loop->the_post();
echo '<option selected value="'.the_title().'</option>';
endwhile;
?>
</select>
Now I want to get the products Image from this query. Is it possible to get the image also from this code or is there any other solution for this?

You just need to use get_the_post_thumbnail():
echo get_the_post_thumbnail($loop->post->ID, 'yourTable');
Inside the body of while()
Example:
while ($loop->have_posts()) : $loop->the_post();
echo get_the_post_thumbnail($loop->post->ID, 'yourTable');
endwhile;

Related

Woocommerce php array select dropdown only shows 10 results

I have this product dropdown for Woocommerce.
Problem is: it does only display 10 product..
I would like it to show all of my products!
Can anyone guide me to the problem :-) ?
Dropdown:
<option value=""> - Select poster - </option>';
$args = array( 'post_type' => 'product' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) :
$loop->the_post();
$post_id = $loop->post->ID;
$product = wc_get_product($post_id);
$image_id = $product->get_image_id();
$image_url = wp_get_attachment_image_url( $image_id, 'full' );
$title = $product->get_name();
$permalink = $product->get_permalink();
echo '<option value="'.$image_url.'" producturl="'.$permalink.'".>'.$title.'</option>';
endwhile;
// Reset post data
wp_reset_postdata();
echo'</select>
Note: A jQuery scripts sorts the dropdown alphabetically, and another script makes next and previous buttons for the dropdown - but i guess thats not the problem..
solved: just WP settings -> max post pr page was set to 10

In wordpress how to get post loop inside another loop

I want to get latest post based on author loop.
this is my code:
function display_artist_func() {
$out_args = array(
'post_type' => 'product',
'author' => 1,
'showposts' => 3
);
$out_loop = new WP_Query($out_args);
while ($out_loop->have_posts()) :
?>
<div><?php echo get_avatar( get_the_author_email(), '80' ); ?> </div>
<?php
//$product = wc_get_product( $product->id );
$args = array(
'post_type' => 'product',
'showposts' => 10,
'orderby' => 'date',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;$author = get_user_by( 'id', $product->post->post_author ); ?>
<li>
<?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>
<h4><?php echo $author->display_name;?></h4>
<div><?php echo get_avatar( get_the_author_email(), '80' ); ?> </div>
<?php
//echo $product->get_price_html();
//woocommerce_template_loop_add_to_cart( $loop->post, $product );
?>
</li>
<?php
endwhile;
endwhile;
wp_reset_query();
}
add_shortcode( 'display_artist', 'display_artist_func' );
there is 2 loop:
outside loop get the 3 author.
inside loop get the 3 post based on author from outside loop.
in outside loop a want to display author avatar.
but this code not work.
Your problem is that you're never setting up the post data in your outer loop. It looks to me like this code creates an endless loop? The function get_the_author_email() is actually deprecated, but what its doing is using global $authordata.
In your inner loop, you call:
$loop->the_post();
which is going to take your current query, setup postdata for the active post and then remove it so it no longer returns in your call to have_posts(). You're missing a call to:
$out_loop->the_post();
And so your postdata isn't setup, and you're going to create an endless loop.
NOW with all that said
To be perfectly honest your code doesn't look like it makes a lot of sense. Your 'outer loop' is querying products with one specific hard-coded author ID, but the only thing that outer post is used for IS to get the author email. The way its written - the author is going to be the same for every run of $out_loop.
So I think you still may be missing some logic here - I don't know from your description exactly how its supposed to work, but as is - if you're just looping through products, there's no need for two loops here.

Wordpress loop using wp_query category_name => category+textstring

I have a wp_query getting all posts in a category with the same slug as the title as the current page.
Where i have become stuck is modifying wp_query category_name => $post->post_name plus a text string.
For example if page was to be called "Long Day" all posts with the category slug "long-day" will be shown. I need to bring in these posts in one loop and in another have post_name plus the text sting eg long-day-today.
This is what i have so far...
<?php
global
$post; $args = array(
'category_name' => $post->post_name
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="entry"><?php the_content(); ?></div>
<?php echo $cat ?>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
[EDIT]
I feel like a bit of a fool now, after a little playing i came up with this
global
$post;
$exclude = "hello";
$args = array(
'category_name' => "{$post->post_name} . $exclude"
);
Which seems to do the trick, if there is a better way of achieving this i would still be interested to hear...
Thanks
global
$post;
$exclude = "hello";
$args = array(
'category_name' => "{$post->post_name} . $exclude"
);

Am looking for a way to show random products in products category page using woocomerce

How do I change product in same category randomly? I've been looking but can't seem to find any plugin/script to do this, anyone got an idea on this... thanks
You can use the following code to display the products rows of "CATXXX" :
<?php echo do_shortcode( '[product_category category="CATXXX" per_page="8" columns="4" orderby="rand"]' ) ?>
Also you can treat products like any other post type and query using get_posts(). Limit the number of posts retrieved to 1 and change the orderby parameter to random:
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'category' => 'CATXXX'
'post_type' => 'product');
$random_products = get_posts( $args );
foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();
You can also use new WP_Query() which would be similar.
You can try this code shortcode
[product_category category="category_slug" per_page="10" orderby="rand"]
Or
<?php echo do_shortcode( '[product_category category="category_slug" per_page="10" orderby="rand"]' ) ?>

Wordpress Query by Tag(Priority) then Category(backfill)

I've search high and low but have been unable to find a solution to my problem. Hopefully this isn't a duplicate question that I was unable to find through search here and on google.
I'm attempting to have a wp_query return a set number of results (10) that are populated by any posts found in the current pages category. I was able to accomplish this with...
$postCategories = get_the_category();
$atts = array (
'posts_per_page' => 10,
'tag' => 'sticky',
'category_name' => $postCategories[0]->slug,
);
But where I'm having trouble is with the tag. I would like any posts that have the tag 'sticky' to take priority over any of the posts being brought in by the category match while not adding any more than 10 results still.
Any help or guidance would be much appreciated as i'm kind of a newbie to php. Thanks
I think this could work for you, but it's hard to know for sure without knowing the specifics of your project.
<ul>
<?php $sticky = get_option( 'sticky_posts' ); // Get sticky posts ?>
<?php $args_sticky = array(
'post__in' => $sticky,
'posts_per_page' => 10, // Limit to 10 posts
'ignore_sticky_posts' => 1
); ?>
<?php $sticky_query = new WP_Query( $args_sticky ); ?>
<?php $sticky_count = count($sticky); // Set variable to the number of sticky posts found ?>
<?php $remaining_posts = 10 - count($sticky); // Determine how many more non-sticky posts you should retrieve ?>
<?php if ($sticky_count > 0) : // If there are any sticky posts display them ?>
<?php while ( $sticky_query->have_posts() ) : $sticky_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data ?>
<?php if ($remaining_posts > 0) : // If there are non-sticky posts to be displayed loop through them ?>
<?php $postCategories = get_the_category(); ?>
<?php $loop = new WP_Query( array( 'post_type' => 'post',
'posts_per_page' => $remaining_posts,
'post__not_in' => get_option( 'sticky_posts' ),
'category_name' => $postCategories[0]->slug
) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
<?php wp_reset_query(); // Restore global post data ?>
<?php endif; ?>
</ul>

Categories