Using tax_query to get single post per category
-
I have a list of magazines I need to categorize and sort. I have created a custom post type and registered a custom taxonomy to organize them.
Category Structure:
Print Magazines (parent category)
—- Animals (child category)
——– Dog (magazine)
——– Cat (magazine)
——– Bird (magazine)
—- Sports (child category)
——– Baseball (magazine)
——– Football (magazine)
——– Soccer (magazine)I need to query the print-magazine category and create a list of print magazine categories [animals, sports, tech, …]. Each child category will use the first magazines cover image as the child categories thumbnail. That thumbnail will link to a page that lists all magazines for that category. That page will link to individual magazine pages.
I can query and get all magazines in the parent category (print-magazines), but am wondering how to return only the first magazine post info per child category or loop through the results of the query to sort it myself how I see fit.
This is my tax query.
$args = array( 'post_type' => 'magazine', 'post_status' => 'publish', 'posts_per_page' => -1, 'tax_query' => array( array( 'taxonomy' => 'magazine-category', 'field' => 'slug', 'terms' => 'print-magazines' ) ) ); $the_query = new WP_Query( $args ); while ( $the_query->have_posts() ) : $the_query->the_post(); $image = get_field('magazine_thumbnail'); if( !empty($image) ) { // render result to page } endwhile;
- The topic ‘Using tax_query to get single post per category’ is closed to new replies.