• I’ve been struggling over this concept for days and am hoping to get some help as I am a beginner at using custom post types.

    I’m setting up a site to display an inventory of showroom products.

    Custom Post Type: Products

    Taxonomies related and their terms:
    Model Year (2014, 2013, 2012)
    Manufacturer (Company 1, Company 2)
    Condition (New, Used)

    In the “New Products” archive, I want to display all products in “New” condition, ordered by their model year with 2014 displaying first.

    I did try adding a custom field (year_model) to be able to orderby the meta_value, although this didn’t work.

    Here is the code I have so far to display all new models.

    <?php get_header();
    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    ?>
    
    <div id="container">
    <div id="content">
    
    <section id="post-<?php the_ID(); ?>">
    <article>
    <div class="entry-content clearfix">
    		<h2 class="entry-title"><?php echo apply_filters( 'the_title', $term->name ); ?> Products</h2>
    <?php
    $locations_query = new WP_Query( array(
    	  'post_type' => 'products',
    	  'posts_per_page' => 15,
    	  //'meta_key'      => 'year_model', // the meta key of custom field
              //'orderby'      => 'meta_value',
              //'order'         => 'ASC',
    'tax_query' => array(
    		  array(
     			 'taxonomy' => 'condition',
    			 'field' => 'slug',
    			 'terms' => 'new'
    		)
    	)
    ) );
    // Display the custom loop
    if ( $locations_query->have_posts() ): ?>
    <ul class="posts">
    <?php while ( $locations_query->have_posts() ) : $locations_query->the_post(); ?>
    <li>
    	<a href="<?php the_permalink(); ?>" rel="bookmark"><h2 class="entry-title"> <?php the_title(); ?></h2>
    	<div class="post-featured-image"><?php the_post_thumbnail(); ?></div></a>
    	</li>
    	<?php endwhile; wp_reset_postdata(); ?>
    </ul><!--// end entry-header -->
    <?php endif; ?>
    </div><!--entry-content-->
    </article>
    </section>
    </div><!--content-->
    </div><!--container-->
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I don’t know what the problem is, you are on the right track though. I suggest you hook the ‘posts_request’ filter and echo out the passed mySQL query string. Often examining this will show why the query is failing. Hopefully what ever is causing the fail will be an indication of how to fix it.

    These queries often contain in part the clause WHERE (1=1), why, I’ve no idea. It of course always resolves to true so is meaningless. Except one time with a similar query I got WHERE (1=0) so no matter what the rest of the query was, it never returned anything. Unfortunately, I can’t remember what I did to fix this. Hopefully you don’t get the same thing, but it does illustrate how this debugging technique can be useful.

Viewing 1 replies (of 1 total)
  • The topic ‘Taxonomy Archive – Sort and Filter by Terms’ is closed to new replies.