Query by Tag first, if not present, return all from a post_type
-
I’ve got a sidebar that depending on where you are on a site, returns records for a custom post type that have a tag whose slug matches the post’s slug. Up until a few days ago, this code worked perfectly, but it now cacks after the else : statement. It still works on my test environment, in fact. I’m wondering if there’s a more elegant/efficient way to do this:
<aside> <div id="whereSidebar"> <?php $selector = basename(get_permalink()); // get the current page's slug $pageTitle = get_the_title(); $args = array( 'post_type' => 'events', 'tag' => $selector, // drop in the current page's slug (I've set up tags with slugs that match those of post slugs so that I can pull related "Events" for specific "Places" or "Activities") 'meta_key' => 'start_date', 'orderby' => 'meta_value', 'order' => ASC, 'posts_per_page' => 4 ); $the_query = new WP_Query( $args ); if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> --- do stuff -- <?php endwhile; else : //query ALL posts from the EVENTS custom post type, not just those tagged to match the value in $selector $general = array( 'post_type' => 'events', 'meta_key' => 'start_date', 'orderby' => 'meta_value', 'order' => ASC, 'posts_per_page' => 4 ); $gen_query = new WP_Query( $general ); if($gen_query->have_posts() ) : while ( $gen_query->have_posts() ) : $gen_query->the_post(); ?> --- do basically the same stuff, but with a larger query result --- <?php endwhile; endif; wp_reset_postdata(); // closes events sub-query?> <?php endif // closes events main query ?> <?php wp_reset_postdata(); //reset the original page query ?> </div> <!--/.whereSidebar--> </aside>
Live site: http://www.kawarthasnorthumberland.ca
Test environment: http://kawarthas.dvntdesign.com
Under Where to Go compare Bobcaygeon and Fenelon Falls
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘Query by Tag first, if not present, return all from a post_type’ is closed to new replies.