php - WP_Query To Display Product Of Brand On Taxonomy Page

admin2025-06-05  0

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();
Share Improve this question edited Dec 4, 2018 at 15:37 honk31 1,4081 gold badge13 silver badges23 bronze badges asked Dec 4, 2018 at 14:27 sotsot 451 silver badge9 bronze badges 4
  • 1 so you are on a taxonomy archive page, lets say, url/pwb-brand/pwb-brand-x, correct? than the default wordpress query should handle the query by itself..? if you need to rund your own query, consider using get_queried_object() to get the query_vars. speaking of query vars, you registered your custom taxonomy to poplulate query vars, correct..? – honk31 Commented Dec 4, 2018 at 14:42
  • If you're on a taxonomy archive for the pwb-brand already then why are you creating a custom query and loop? Is this because you wanted to add posts_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:33
  • @TomJNowell I wanna use the posts_per_page to -1 to print all the products of this brand. – sot Commented Dec 4, 2018 at 21:45
  • That can be done without any of this, but keep in mind that too many posts and the page will never finish loading – Tom J Nowell Commented Dec 5, 2018 at 0:44
Add a comment  | 

1 Answer 1

Reset to default 0

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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749128447a316605.html

最新回复(0)