0

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;

1 Answer 1

-1

Set 'posts_per_page' to 1 on your args variable. I also suggest that you add 'page-attributes' while registering the magazine's post type and order the result by 'menu_order'. This way, you will have the field Order while editing a magazine, and set the highest which should appear on this query.

3
  • If I set 'posts_per_page' to 1 I only get one post returned for all print-magazines. What I need is one post returned for all children of print-magazines. Also 'page-attributes' has a warning about memory issues with posts over 100. I expect I may have at least this many posts.
    – WithAnEs
    Jun 24, 2015 at 12:58
  • Why not query the children category then, instead the parent one? Jun 24, 2015 at 13:03
  • That was one avenue I was going down also. Get a list of child category id's and query those so I can limit to 1 per child category. Wasn't able to figure it out.
    – WithAnEs
    Jun 24, 2015 at 13:08

Your Answer

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

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