• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

List Parent Posts; Exclude Child Post

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › List Parent Posts; Exclude Child Post

This topic is: not resolved

Tagged: category, child, parent, post

  • This topic has 2 replies, 3 voices, and was last updated 11 years ago by Genesis Developer.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • June 23, 2014 at 9:24 pm #111260
    makingyouthink
    Member

    I have been playing around with this code to generate a list of categories and their post. Our design includes both parent and child categories, however, if we show the posts in the Parent Categories, it lists all the parent and child posts. Anyone have any suggestions on how to filter only the Parent Posts.

    Thanks to Lauren @ OnceCoupled for helping me get this far. See the original code at http://www.studiopress.community/topic/display-all-posts-from-all-categories-on-a-single-page/

    <?php
    /**
    * Template Name: Category Index
    * Created by: Lauren Gray Designs (http://oncecoupled.com)
    * Modified by: Mark Hanlon
    * URL: http://www.makingyouthink.ca
    */
    
    /**
    * VERSIONS
    1.0 Created by Lauren Gray Designs
    1.1 Revisions by Mark Hanlon
    1.5 Revised to include page text and page title
    2.0 Revised HTML formatting to remove Catagory from unordered (bulleted) list; assigned as H3
    2.1 Revised to include Parent Catagory and Sub-Catagory
    */
    
    // Remove the loop and replace it with our own
    // get top level terms
    
    remove_action( 'genesis_loop', 'genesis_do_loop' );
    
    // Include Page title and allow for text befor Coded Cagatories
    add_action( 'genesis_loop', 'genesis_standard_loop', 5 );
    
    // Custom Function to display all Parent, Catagories and related Post
    add_action( 'genesis_loop', 'LGD_add_archive', 9 );
    
    function LGD_add_archive() {
    
      // Include category ID as ID, ID to exclude
      $Cat_Exclude = '116';
      $Post_Exclude ='';
      
      $args = array(
    	'exclude' => $Cat_Exclude,
    	'orderby' => 'name',
    	'parent' => 0,
    	'hide_empty'=> TRUE,
      
      );
      
      $parent = get_categories( $args );
      $category = get_the_category();
      $cat = get_category($category[0]->term_id);
      $parent_catid = $cat->category_parent;
      
      
      // List each parent category
      foreach ( $parent as $category ) {
    	echo '<h1><a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a><br/></h1>';
    
    	  // Arguments for Post in Parent - trouble listing only post in Parent category and not to include child category
    	  $args = array(
    		  'exclude' => $Post_Exclude,
    		  'posts_per_page' => -1, 
    		  'category' => $category->term_id,
    		  'include_children' => false,
    		  'depth' =>1,
    		  );
    	
    	  $myposts = get_posts( $args );
    
    	  // List the posts in said category
    	  foreach ( $myposts as $post ) : setup_postdata( $post );
    	//		echo '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>';
    	echo '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) ;
    	  echo '</a> written by ' ;    
    	  the_author_posts_link();
    	  echo ' on ';
    	  echo get_the_date ('F j, Y', $post);
    	  echo '</li>'; 
    	endforeach; 
    	  
    	  // Close everything up
    	  echo '</ul>';
    	
    	$args = array(
    	'exclude' => $Cat_Exclude,
    	  'type' => 'post',
    	  'orderby' => 'name',
    	  'child_of'=> $category->term_id,
    	  'post_status' => 'publish'
    	  );
    	  
    	$categories = get_categories($args);
    	
    	// List each category
    	foreach($categories as $category) { 
    	
    	  // Output the cateogry (and link to it)
    	  echo '<h3><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name . '</a></h3><ul>';
    	  
    	  $args = array(
    		  'exclude' => $Post_Exclude,
    		  'posts_per_page' => -1, 
    		  'category' => $category->term_id );
    	
    	  $myposts = get_posts( $args );
    	  
    	  // List the posts in said category
    	  foreach ( $myposts as $post ) : setup_postdata( $post );
    	//		echo '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>';
    	echo '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) ;
    	  echo '</a> written by ' ;    
    	  the_author_posts_link();
    	  echo ' on ';
    	  echo get_the_date ('F j, Y', $post);
    	  echo '</li>'; 
    	endforeach; 
    	  
    	  // Close everything up
    	  echo '</ul>';
    	}
    	}
    }
    
    genesis();
    http://www.test-me.ca/index-catagories-v2/
    June 23, 2014 at 11:04 pm #111266
    Brad Dalton
    Participant

    You'll find the answer here WP_Query


    Tutorials for StudioPress Themes.

    June 24, 2014 at 12:36 am #111268
    Genesis Developer
    Member

    place 'post_parent' => 0 into $args array. You will use it in appropriate position. Then you will get correct result


    Download Genesis Featured Posts Combo Widget | Simple Grid Layouts Plugin for Posts, CPTs and terms
    You can request new tips/help.

  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2025 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble