John Davidson

Woocommerce php array select dropdown only shows 10 results

0 comments
Message:


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..



My Answer:
To display all products in the dropdown instead of just 10, you will need to modify the query that retrieves the products. Here is an example of how you can modify the query in your PHP code:

php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
);

$products = new WP_Query($args);

if ($products->have_posts()) {
echo '';
} else {
echo 'No products found';
}

wp_reset_postdata();


In this code snippet, we are setting the `posts_per_page` parameter to `-1`, which means that all products will be retrieved. Then we loop through the products and output them as options in the select dropdown.

Make sure to replace any existing code that generates the dropdown with this modified code. This should display all products in the dropdown instead of just 10.

Rate this post

4 of 5 based on 8902 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us