0

I have a Custom Post Type for "Products" with a Custom Taxonomy of "Product Line." I've set up a page to list out the Products by their Product Line. However, we're also using a Product Line of "Featured Products" to use on the Homepage only. We do not want "Featured Products" to display within the "All Products" page. I have the following loop.

  <?php 
    $post_type = 'products';
    $taxonomies = get_object_taxonomies( $post_type );

    foreach ($taxonomies as $taxonomy){
        $terms = get_terms($taxonomy, array('orderby' => 'date', 'order' => 'ASC'));

        if ( !empty( $terms ) && !is_wp_error( $terms ) ){
            foreach ( $terms as $term ) {
                $args = array(
                    'post_type'           => $post_type,
                    'orderby'             => 'date',
                    'order'               => 'ASC',
                    'ignore_sticky_posts' => 1,
                    'post_status'         => 'publish',
                    'posts_per_page'      => - 1,
                    'tax_query'           => array(
                        array(
                            'taxonomy'    => $taxonomy,
                            'field'       => 'slug',
                            'terms'       => $term->slug,
                            'operator'    => 'IN'
                        ),
                        array(
                            'taxonomy'    => $taxonomy,
                            'field'       => 'slug',
                            'terms'       => 'featured-products',
                            'operator'    => 'NOT IN'
                        )
                    )
                );
                $my_query = null;
                $my_query = new WP_Query($args);
                if ($my_query->have_posts()) {
                    echo '<div class="resource"><h2>' . $term->name . '</h2>';

                        while ($my_query->have_posts()) : $my_query->the_post(); ?>

                            <ul>
                              <li>
                                <div class="resource_image_helper">
                                    <a href="<?php the_permalink(); ?>"><img src="<?php the_field('mobile_image'); ?>"></a>
                                </div>
                                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                <ul>
                                    <?php
                                      // Grab the title and change for slug
                                        $title    = get_the_title();
                                        $stripped = sanitize_title_with_dashes($title);

                                      $literature = do_shortcode('[downloads category='.$stripped.']');
                                      $photos     = get_field('tab_photos');
                                    ?>  

                                    <?php if(!empty($photos)): ?> 
                                        <li>
                                          <a href="<?php the_permalink(); ?>">Photos</a>
                                        </li>
                                    <?php endif; ?>

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

                                    <?php if(!empty($literature)): ?> 
                                        <li>
                                          <a href="<?php the_permalink(); ?>">Literature</a>
                                        </li>
                                    <?php endif; ?>

                                </ul>
                              </li>
                            </ul>

                        <?php
                        endwhile; 
                    echo '</div>';
                } // END if have_posts loop
                wp_reset_query(); 
            } // END foreach $terms
        }
    }
  ?>

The problem I'm having is with the tax_query portion. I do not want to show the "Featured Products" term, which I've done. But with this code, it doesn't show any products that are withing "Featured Products." I still need the Products to show under other Product Line terms, I just need the Featured Products term to not be displayed. Any ideas?

1
  • You know, you could simply use post meta for featured posts like everyone does to distinguish special posts. That is not a loop. You could just perform one query and then sort and manipulate your resulting array using php functions to run your for loop. Right now you are performing (all the taxonomies * all the terms) number of queries. Mar 2, 2015 at 7:50

1 Answer 1

0

I'm not sure how you set up looks, but it does seem that you have a lot of taxonomies registered to your post type, so I don't think it will be very viable to use the the exclude parameter in get_terms() to exclude the specific term.

What I would suggest is, inside your foreach loop before you declare your query arguments, add the following line

if ( $term->slug == 'featured-products' )
    continue;

This will skip the specific term from being queried. You should also delete the second array inside your tax_query where you exclude the featured products term. This will ensure that you still get featured products under the other terms

Just a note, your query is quite expensive and you are hitting the db quite hard. I would suggest that you look at caching the results to optimize the query. You can also have a look at transients

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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