Difficulties with new wp_query
-
I’m going to try to keep this as simple as possible. I am trying to create a page where I loop through the posts in a custom post type, and filter the based on custom taxonomy. For instance, my custom taxonomy is “Topic”, and my custom post type is called “Affiliate”. What I am trying to accomplish is the following:
Website Development (Custom taxonomy term)
Bluehost (Custom Post Type entry)
Godaddy (Custom Post Type entry)
Wordpress (Custom Post Type entry)Insurance (Custom taxonomy term)
Obamacare (Custom Post Type entry)
Etc. (Custom Post Type entry)I am trying to accomplish this on the archive page for my custom post type, and code I have set up for it is structured as follows:
<?php //Get the post type and custom taxonomies (in an array) $type = get_post_type(); $termsargs = array('orderby'=>'name','order'=>'desc','hide_empty'=>'true','fields'=>'all'); $terms = get_terms( 'Topic', $termsargs ); //Yields term objects //Loop through the terms, build a new query based on each term, and run a loop based on that query foreach ($terms as $term) { $args=array( 'post_type' => 'affiliates', 'post_status' => 'publish', 'posts_per_page' => -1, 'ignore_sticky_posts' => 1, 'tax_query' => array( 'taxonomy' => 'Topic', 'field'=>'id', 'terms' => $term->term_id) ); $the_query = new WP_Query( $args ); ?> <?php if ( $the_query->have_posts() ) : ?> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> something happened! yaay! <?php endwhile; ?> <?php wp_reset_postdata(); ?> <?php else: ?> <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p> <?php endif; ?> } ?>
I’m running a foreach loop to loop through the terms, then taking those terms and using them to create a query based on that term to list all the custom posts attached to that term. When I try it, the page will not load. There’s a fatal error somewhere that crashes the page (which I can’t figure out because the error log on my site won’t work).
I have noticed that when I remove the while loop within the foreach loop, the page loads normally, so I believe it is my query and the loop that are crashing everything. I’m convinced this is either me missing something obvious, in which case I apologize sincerely. OR, this has to do with how queries work. If someone could take a look at my code and tell me if they spot anything i’m doing wrong, or has any advice as to how I could more easily accomplish what i’m going for, I would be greatly appreciative.
Thanks very much for your time.
- The topic ‘Difficulties with new wp_query’ is closed to new replies.