Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Loop into Sub Categories and show all posts WordPress



What I'm trying to achieve is a custom loop that will go into my Parents Category / Sub Category and display all post inside the sub category. The main idea of this is to show racing pigeons that are paired per year, would it also be possible to select say one bird next year and use it in multiple years / sub catagories

Example : Bird 1 is in Category 2016 Pairs -> Sub Catagory (Pair One) and then use this same bird also in 2017 ?

2016 Pairs (Parent Category)
|-> Pair One (Sub Category)
|- > Racing Pigeons One (Custom Post)
|- >Racing Pigeon Two (Custom Post)

Overall All I want to show is this once the loop has completed

Pair One, Pair Two etc,
| (show any post inside the sub category and linkable to post page)

// Here is my custom function for custom post section "My Pigeons"

// #### MY PIGEONS - Post & Meta Box ####
// Registers the new post type and taxonomy

function wpt_event_posttype() {
register_post_type( 'events',
'taxonomies' => array('category', 'post_tag'), // this is IMPORTANT
'labels' => array(
'name' => __( 'My Pigeons' ),
'singular_name' => __( 'My Pigeon' ),
'add_new' => __( 'Add New Pigeon' ),
'add_new_item' => __( 'Add New Pigeon' ),
'edit_item' => __( 'Edit Pigeon' ),
'new_item' => __( 'Add New Pigeon' ),
'view_item' => __( 'View Pigeon' ),
'search_items' => __( 'Search Pigeon' ),
'not_found' => __( 'No events found' ),
'not_found_in_trash' => __( 'No events found in trash' )
'public' => true,
'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ),
'capability_type' => 'post',
'rewrite' => array("slug" => "events"), // Permalinks format
'menu_position' => 5,
'register_meta_box_cb' => 'add_events_metaboxes'

add_action( 'init', 'wpt_event_posttype' );
add_action( 'add_meta_boxes', 'add_events_metaboxes' );

// Metabox
// Add the Events Meta Boxes

function add_events_metaboxes() {
add_meta_box('wpt_events_location', 'Event Location', 'wpt_events_location', 'events', 'side', 'default');

//Metabox HTMl
// The Event Location Metabox

function wpt_events_location() {
global $post;

// Noncename needed to verify where the data originated
echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

// Get the location data if its already been entered
$location = get_post_meta($post->ID, '_location', true);

// Echo out the field
echo '<input type="text" name="_location" value="' . $location . '" class="widefat" />';


// Save the Metabox Data

function wpt_save_events_meta($post_id, $post) {

// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;

// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;

// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though.

$events_meta['_location'] = $_POST['_location'];

// Add values of $events_meta as custom fields

foreach ($events_meta as $key => $value) { // Cycle through the $events_meta array!
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value);
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank


add_action('save_post', 'wpt_save_events_meta', 1, 2); // save the custom fields


Answers (2)


Rempty answers:

Do you want list in the breeders page

Year 2016
Pair One
Pigeones One
Pigeones Two

Year 2017
Pair One
Pigeones One
Pigeones Two


You can use a post to multiple categories, there is no problem with that.

I think you must create a custom taxonomy, because you are using the same for posts

Daryl Baker comments:

It's pretty close yes, least I think lol

Let me explain a bit more now that things are moving along :)

When we hit the Breeders pg it will show the pairs > posts inside each category

For Example right now I have,

2016 Pairs (Parents)
- Pair One (Sub Category)
- Multiple Posts (Birds)

All I would like to show is this once the loop is done and each sub category should be grouped with the posts not just showing all in a mash up.

Pair One (Sub Category)
- All Post inside that

Pair Two (Sub Category)
- All posts inside that

Etc etc

The problem I find right now with the testing I'm doing is its just showing all the pairs posts all in one big mash up and not sorting them within the Pairs One , Pairs Two etc , it should group them nicely

This something we can do easily ?
Can increase cost if its more work, just need it done soon!


Rempty comments:

Ok try this code

$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => 0,
) );
foreach ( $categories as $category ) {

$catschild= get_categories(array(
'order' => 'ASC',
'parent' => $category->term_id
foreach ( $catschild as $childcat ) {
$query_pidgeons=new WP_Query(array(
'order' => 'ASC',
echo '<h3>'.$childcat->name.'</h3>';
echo '<a href="'.get_permalink().'">'.get_the_title().'</a>';

Daryl Baker comments:

This works perfect! You nailed it!

The only thing I see which isn't broken just a setting I assume is if I click says 2016> Pair One > Bird One and add it to 2017 > Pair Two it also shows that in the current loop. (2016 and 2017)

Is it possible to show only 2016 current pairings ? Other than that you've nailed it dead on :) Great work!


Attached photo to show what I got setup in WP