1

I used this code to get all the products of a specific category, but how can I show only the author's products of a specific category?

<div class="author_products">
    <ul class="author_pubproducts">
        <?php
        $args = array(
            'post_type' => 'product',
            'posts_per_page' => 12,
            'product_cat' => 'pants'
        );
        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
            while ( $loop->have_posts() ) : $loop->the_post();
                woocommerce_get_template_part( 'content', 'product' );
            endwhile;
        } else {
            echo __( 'No products found' );
        }
        wp_reset_postdata();
        ?>
    </ul>
<div>
  • im putting this code inside the woocommerce>single-product.php page – user85376 Dec 15 '15 at 17:39
1

Pass author data to the query. It should be that hard.

Author Parameters

Show posts associated with certain author.

  • author (int) - use author id.
  • author_name (string) - use 'user_nicename' - NOT name.
  • author__in (array) - use author id (available since Version 3.7).
  • author__not_in (array) - use author id (available since Version 3.7).

Of course, I don't know what page you are on or how you are determining, or needing to determine, the "author" so I'm not quite sure how to guess at the code.

0

As @s_ha_dum mentioned, you can just add additional query args parameter.

I assume, you wish to display single author products on the profile page of that author.

To do that, you would modify your code to look something this (which also fixes HTML, since in a case where there are no products, you are echoing text without

  • element within a list):

    <?php
    
    $authorID = get_the_author_meta('ID');
    
    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish'
        'posts_per_page' => 12,
        'product_cat' => 'pants'
        'author'    => $authorID
    );
    
    $loop = new WP_Query( $args );
    
    ?>        
    <div class="author_products">
        <?php if ( $loop->have_posts() ) { ?>
            <ul class="author_pubproducts">
            <?php while ( $loop->have_posts() ) : $loop->the_post();
                woocommerce_get_template_part( 'content', 'product' );
            endwhile; ?>
            </ul>
            <?php
            } else {
                echo __( 'No products found', 'mytheme' );
            }
            wp_reset_postdata();
        ?>
    <div>
    

    Again, this would work if you are using this piece of code on author page, otherwise you would need to retrieve and assign Author ID to variable $authorID using another method. For example, if you would be using ACF in some way, then it would be:

    $authorID = get_field('author_id_field');
    

    Hopefully that was helpful and will provide you some ideas.

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

    Not the answer you're looking for? Browse other questions tagged or ask your own question.