WP_Query To Display Product Of Brand On Taxonomy Page

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.

Doing it With Filters and Actions

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 );

Caveats

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:

  • pagination
  • lazy loading
  • infinite scroll

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