wordpress - WP_Query and Woocommerce -
i have function inside (*wordpress child theme) functions.php returns attached woocommerce product categories, using global $wp_query. get_posts() function return number of products first page of products. (*the post_per_page value - in case 16).
i have tried temporarily set post_per_page -1, adding code below right before function call in archive-product.php template:
$wp_query->set('posts_per_page', 999); $wp_query->query($wp_query->query_vars);
and resetting value after function call inside archive-product.php template
$wp_query->set('posts_per_page', 16); $wp_query->query($wp_query->query_vars);
this works, messes pre_get_posts function (*which sorts products), , seems cause issues listing of product results if on 500 products?
please suggestions 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 $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 $option) { $x .= $option; } $x .= '</select>'; return $x; }
try replacing $my_posts = $wp_query->get_posts();
following:
// current query vars $my_query_args = $wp_query->query_vars; // disable paging - return results $my_query_args['nopaging'] = true; // create new query modified query vars $my_query = new wp_query($my_query_args); $my_posts = $my_query->get_posts();
Comments
Post a Comment