I am trying to print all products of a brand on a taxonomy page.
I find the wp_querry but i cant print the current brand name of the brand to show all products in this page.
What to put on terms array to print the current page brand name?
// get products
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'pwb-brand',
'field' => 'name',
'terms' => array ('NAME?????')
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'not found anyhting.' );
}
wp_reset_postdata();
I am trying to print all products of a brand on a taxonomy page.
I find the wp_querry but i cant print the current brand name of the brand to show all products in this page.
What to put on terms array to print the current page brand name?
// get products
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'pwb-brand',
'field' => 'name',
'terms' => array ('NAME?????')
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'not found anyhting.' );
}
wp_reset_postdata();
You could get the queried object, but that still leaves the unmentioned problem that you're discarding the main query and doing a second one, making the page at a minimum 2x slower. Pagination is also broken by this.
You may have seen people warn you against using query_posts
, this is why, using WP_Query
the same way is just recreating the insides of that function.
Instead, use the pre_get_posts
action to modify the main post request before WP goes to the database.
e.g. here is an example that excludes the category with id 1
from the homepage:
function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-1' );
}
}
add_action( 'pre_get_posts', 'exclude_category' );
So instead, lets replace the check with something like this:
if ( $query->is_tax( 'pwb-brand' ) && $query->is_main_query() ) {
And set the posts_per_page
value, e.g.
$query->set( 'posts_per_page', -1 );
By setting this to -1
the worst case scenario has gotten significantly worse, and there is now no upper limit on how slow or expensive this page will be. So much slow, you may run out of execution time, or memory trying to display it.
You might think "But there's only 50 products that'll never happen", to which I would say, set a stupidly high number you never expect to hit, but never -1
/everything.
Alternatives include:
Some people display everything because they want to be able to do ctrl+f
and find a product easily, but this can be done with a search box and a hidden input with the name post_type
and the value product
, and a second that sets the taxonomy to pwb-brand
get_queried_object()
to get thequery_vars
. speaking of query vars, you registered your custom taxonomy to poplulate query vars, correct..? – honk31 Commented Dec 4, 2018 at 14:42posts_per_page
set to-1
but instead of modifying the main query, you replaced it, leading to this problem? Or modify the post types fetched? Is there a particular reason you didn't ask about the original problem instead? This looks like you're asking for a solution to a problem with a solution to a problem, an XY problem – Tom J Nowell ♦ Commented Dec 4, 2018 at 17:33posts_per_page
to-1
to print all the products of this brand. – sot Commented Dec 4, 2018 at 21:45