WordPress.org

Ready to get started?Download WordPress

Forums

WP Query (19 posts)

  1. TA003
    Member
    Posted 1 year ago #

    Hi,

    Having trouble with wp_query 'category__and'. Cant get it to work.
    This is my code:

    $args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'category__and' => array( 20, 61, 74, 28 ), 'orderby' => 'date' );
    $loop = new WP_Query( $args );
    
    while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
          // Info comes here.
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>

    Any idea what i'm doing wrong?
    Tanks.

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    It doesnt return any posts? Do you have posts that have all the categories assigned to it?

  3. sharky1989
    Member
    Posted 1 year ago #

    Fatal error: Class 'WP_Query' not found on line 2

  4. TA003
    Member
    Posted 1 year ago #

    I have a post that is assigned to all categories. I have tried it with one or two categories but it gave no result.

    What can i get the posts without an error?
    The query works when i remove:'category__and' => array( 20, 61, 74, 28 ), but i need to get the posts that ar assigned to different categories. It's an online shop with a search module... You can select a color, a size, ...

  5. esmi
    Forum Moderator
    Posted 1 year ago #

    @sharky1989: It is extremely impolite to interrupt another poster's ongoing thread with a question of your own. It causes significant problems for the forum's volunteers and prevents us from being able to track issues by topic. Please post your own topic.

  6. TA003
    Member
    Posted 1 year ago #

    But this is my own post?

  7. keesiemeijer
    moderator
    Posted 1 year ago #

    I think she meant sharky1989.

    Does 'category__in' work?
    Do you have any other loops in the theme template file.

    Can you post the full code of the template file - See theForum Rules on how to post a large excerpt of code.

  8. esmi
    Forum Moderator
    Posted 1 year ago #

    Sorry! My bad! :-(

  9. TA003
    Member
    Posted 1 year ago #

    You can find the full code at this link:
    http://pastebin.com/CKeGZisF

    @keesiemeijer
    i have tried 'category__in', that does not work either.

  10. keesiemeijer
    moderator
    Posted 1 year ago #

    Maybe the posts are fetched but the shortcode doesn't work properly. try changing this:

    $loop->post->ID

    to this:

    $post->ID
  11. TA003
    Member
    Posted 1 year ago #

    That also does not work.
    The code is fine i think becaus when i change the query to:

    $args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'orderby' => 'date' );

    I left out the 'categorie_and' and it works. So i think it is the query that is not wright? But i can't seem to figure out what is wrong.

  12. keesiemeijer
    moderator
    Posted 1 year ago #

    Very strange.
    Can you paste this inside your loop to see if post titles are showing up.

    <?php the_title( '<h3>', '</h3>' ); ?>
  13. TA003
    Member
    Posted 1 year ago #

    that doesn't change a thing. I can't see the titles.

  14. TA003
    Member
    Posted 1 year ago #

    Print_r from the loop:
    http://pastebin.com/9Huj0kme

    Maybe this will help?

  15. keesiemeijer
    moderator
    Posted 1 year ago #

    I'm not sure why this isn't working if you have a post with all categories assigned. Try it with a tax query:

    $args = array(
    	'post_type' => 'product',
    	'posts_per_page' => 10,
    	'orderby' => 'date',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'category',
    			'field' => 'id',
    			'terms' => array( 20, 61, 74, 28 ),
    			'operator' => 'AND'
    		)
    	)
    );
    $loop = new WP_Query( $args );

    http://codex.wordpress.org/Function_Reference/WP_Query#Taxonomy_Parameters

  16. keesiemeijer
    moderator
    Posted 1 year ago #

    Maybe this will help?

    Yes it does. The query doesn't include your category query by looking at the 'request' and no posts were found.

    I think a plugin or theme is filtering the query. What plugin are you using for the product posts?

  17. TA003
    Member
    Posted 1 year ago #

    I'm using wooCommerce.

    I've tried:

    $args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'baby', 'orderby' => 'rand' );

    This is working but it shows me items form the first category?
    I've tried:

    $args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'baby, red', 'orderby' => 'rand' );

    But it seems to show only the products from the first category.

  18. TA003
    Member
    Posted 1 year ago #

    I found it! I've used your code and changed the taxonomy to 'product_cat'. This is the full query:

    $args = array(
    'post_type' => 'product',
    'posts_per_page' => 10,
    'tax_query' => array(
    	array(
    	'taxonomy' => 'product_cat',
    	'field' => 'id',
    	'terms' => array(20, 61, 74, 28),
    	'operator' => 'AND'
    	)
    	),
    	'orderby' => 'date',
    	'order' => 'ASC'
    );
    $loop = new WP_Query( $args );

    Thanks for all the help!

  19. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome. I'm glad you've got it resolved :-)

    It was not a category but a custom taxonomy that was the problem.
    http://codex.wordpress.org/Taxonomies#Custom_Taxonomies

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags