Reputation: 71
I have a function inside (*Wordpress Child Theme) functions.php which returns the attached WooCommerce product categories, using global $wp_query. The get_posts() function will only return the number of products for the first page of products. (*the post_per_page value - in this case 16).
I have tried to temporarily set the post_per_page to -1, by adding the code below right before my function call in archive-product.php template:
$wp_query->set('posts_per_page', 999);
$wp_query->query($wp_query->query_vars);
and then resetting the value after the function call inside archive-product.php template
$wp_query->set('posts_per_page', 16);
$wp_query->query($wp_query->query_vars);
This almost works, but messes up the pre_get_posts function (*which sorts the products), and also seems to cause issues with the listing of the product results if over 500 products?
Please any suggestions would be greatly appreciated. Thanks.
//build dynamic category select menu based on attached categories
function dynamic_category_select() {
global $wp_query;
$my_posts = $wp_query->get_posts();
$my_post_ids = wp_list_pluck($my_posts, 'ID');
$categories = wp_get_object_terms($my_post_ids, 'product_cat');
foreach($categories as $category) {
$options[] = '<option value="' . $category->slug . '">' . $category->name . '</option>';
}
$x = '<select id="category-options" name="category-options">';
$x .= '<option value="">Select Category Options</option>';
foreach($options as $option) {
$x .= $option;
}
$x .= '</select>';
return $x;
}
Upvotes: 1
Views: 966
Reputation: 1406
Try replacing your $my_posts = $wp_query->get_posts();
with the following:
// Get current query vars
$my_query_args = $wp_query->query_vars;
// Disable paging - return all results
$my_query_args['nopaging'] = true;
// Create a new query with the modified query vars
$my_query = new WP_Query($my_query_args);
$my_posts = $my_query->get_posts();
Upvotes: 0