Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom post type 'products' and custom taxonomy of 'maker' that i have connected to that cpt 'products'. I have written this code :

  <?php $myproducts = new WP_Query(array('post_type' => 'products', 'maker'=>'samsung')); ?>
  <?php while($myproducts->have_posts()) : $myproducts->the_post(); ?>

  <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>

  <?php endwhile; ?>
  <?php wp_reset_postdata(); ?>

and this allows me to loop products from one of added makers (in this case samsung). What I am actually trying to do is to create a page that will loop through all of items from 'maker' taxonomy that a client might add to it. It would be great if I could use these elements of the 'maker' taxonomy to show lists of all 'products' that belong to those makers. Basically loop the taxonomy elements and use those results to loop all products that belong to those makers. Sorry if i'm being redundant, I'm just trying to be as specific as a beginner might be. Thank you all.

share|improve this question

2 Answers 2

You can simply use a tax query to get all posts attached to any product_cat term:

$args = array(
    'post_type' => 'product',
    'tax_query' => array(
        array(
            'taxonomy' => 'product_cat',
            'terms' => get_terms( 'product_cat', 'fields=ids' ),
        )
    ),
    'orderby' => 'menu-order',
);
share|improve this answer

Use this code to get product by category.

<?php $args = array( 'post_type' => 'product','product_cat' =>'sumsung', 'orderby' => 'menu-order' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<?php the_post_thumbnail(); ?>
<?php the_title();?>           
<?php the_excerpt(); ?>
<a href="<?php the_permalink(); ?>"></a>  
<?php endwhile;?>
share|improve this answer
    
thanks for trying Shafi, I tried your approach, but im getting an error, it seems its from the key-value pair that you have 3 items in there product_cat => maker => samsung.. –  user44772 Jan 16 at 11:55
    
@user44772 I remove 'maker' on above code. check it again. –  Shafi Zahid Jan 16 at 12:00
    
..tried it again, I get a list of all products from my product custom post type. But that was not my problem, i wanted to print a list of all makers with all products that go along with those maker taxonomies. I appreciate the effort –  user44772 Jan 16 at 12:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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