WordPress.org

Ready to get started?Download WordPress

Forums

Get Parent Category Posts, Not Sub-Categories Posts Unless Belong to Parent Also (16 posts)

  1. Jennifer
    Member
    Posted 3 years ago #

    I'm not sure if I worded my topic title properly, which is probably why I haven't been able to search for the answer to my problem just yet (oh, have I tried, just can't find the right keywords I suppose). Anyways, I am trying to get a simple array of all items that belong to a category, but I am having parent / children issues.

    Here is a link to my website http://www.thelooksforless.com/. I want the slider at the top to show the "Celebrity Looks For Less" posts only! Instead it is showing an Ashlee Simpson post as well because Ashlee Simpson (child) is in the "Celebrity Looks for Less" (parent) category. The Fendi Spy Bag post should not show up there because it does not belong to the "Celebrity Looks For Less" category, it belongs to it's child category "Ashlee Simpson".

    My code is:

    <?php
    	$slidecat = get_option('scar_gldcat');
    	$slidecount = get_option('scar_gldct');
    
    	$my_query = new query_posts('cat=4')       WP_Query('category_name= '. $slidecat .'&showposts='.$slidecount.'');
    while ($my_query->have_posts()) : $my_query->the_post();$do_not_duplicate = $post->ID;
    ?>
    
    <div class="panel">
    
    <?php $screen = get_post_meta($post->ID, 'screen', $single = true); ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?> - Get The Celebrity Look For Less"><img src="<?php echo $screen; ?>" width="200" height="100" alt="<?php the_title(); ?> - Get The Celebrity Look For Less" /></a>
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?> - Get The Celebrity Look For Less"><?php echo $category->cat_name . ' ';} ?></a></h2>
    </div>
    
    <?php endwhile; ?>
  2. Jennifer
    Member
    Posted 3 years ago #

    I need to accomplish this for a main loop as well. My slider is displaying a post that it should not, as described above and if you look at this category page http://www.thelooksforless.com/category/celebrity-looks-for-less/, the 3rd post should not be there. It is showing up because it is in the Ashlee Simpson category, which is in the Celebrity Looks for Less category.

    So I believe I have two separate but similar problems. A secondary loop issue (the slider) and a main loop issue (the category page). I only want "Celebrity Looks for Less" category posts to show up on those pages. Not posts that are associated with children of the parent category.

    Thanks in advance for your help!

  3. Jennifer
    Member
    Posted 3 years ago #

    this is so so frustrating. i think i've searched every possible combination of keywords for the solution, and i feel it must be an easy solution. i believe, for my slider, that i have to use get_posts so that i do not conflict with my main loop:

    <ul>
    <?php
    
    $myposts = get_posts("category=1");;
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>

    however, it is still showing posts that are not in category 1. category 1, the parent category, is "Celebrity Looks for Less". it contains a child category, "Ashlee Simpson", and the post showing up is in the "Ashlee Simpson" category. Since that post is not "in" category 1, I want it to not appear.

  4. Jennifer
    Member
    Posted 3 years ago #

    ok, so i figured out something that will work. i used query_posts, which is supposed to be used for the main page loop only, if i understand it correctly, and it did effect my main loop; however, i reset the query at the end of it in the header so my home page acts normally. i will use the same query on my category pages to exclude those children posts that do not belong to the parent category.

    <?php
    	$slidecat = get_option('scar_gldcat');
    	$slidecount = get_option('scar_gldct');
    
    	query_posts(array('posts_per_page' => $slidecount,'category__in' => array(1)));
    
    	while(have_posts()) { the_post();
    ?>
    
    <div class="panel">
    
    <?php $screen = get_post_meta($post->ID, 'screen', $single = true); ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?> - Get The Celebrity Look For Less"><img src="<?php echo $screen; ?>" width="200" height="100" alt="<?php the_title(); ?> - Get The Celebrity Look For Less" /></a>
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?> - Get The Celebrity Look For Less"><?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' ';}  ?></a></h2>
    </div>
    
    <?php } ?>
    
    <?php
    //Reset Query
    wp_reset_query();
    
    ?>
  5. Jennifer
    Member
    Posted 3 years ago #

    and lastly, i put this code at the top of my archive post to show only posts in the parent, not the parent's children's posts:

    <?php
    
    $this_category = get_category($cat);
    query_posts(array('category__in' => array($this_category)));
    
    echo $this_category->cat_ID;
    ?>
    
    <?php
    //Reset Query
    wp_reset_query();
    
    ?>
  6. Jennifer
    Member
    Posted 3 years ago #

    ok, strike the code in the last update. this is all the code i needed on the category page to show parent posts only (no reset was needed):

    <?php	
    
    	query_posts(array('category__in' => array($cat)));
    
    ?>

    sorry for all the updates, but hopefully this will help someone else because i know i was desperate and searched everywhere. finally figured it out though.

  7. Jennifer
    Member
    Posted 3 years ago #

    ok, so the above code broke the pagination. the pagination was repeating posts from one page to another. i added 'paged' => $paged to fix the repeating results:

    <?php
    
    	query_posts(array('category__in' => array($cat),'paged' => $paged));
    
    ?>
  8. boxoft.net
    Member
    Posted 3 years ago #

    Thanks for the tip. :)

  9. PaulWarren
    Member
    Posted 3 years ago #

    Thanks!

  10. robbonj
    Member
    Posted 3 years ago #

    Hours and hours... I've beens searching for this solution. Many thanks!

  11. stowepro
    Member
    Posted 3 years ago #

    This was great and worked perfect until the latest update of wordpress that was released the the code...

    <?php
    
    	query_posts(array('category__in' => array($cat),'paged' => $paged));
    
    ?>

    ... stopped working.

    Any idea's?

    One of the pages that I have the code placed.
    http://jetsetlife.tv/destinations/boston/boston-nightlife/

    There are 11 articles linked to the boston category.

    i have another issue, we are wanting to also show every sub category within the Boston Root category something like..

    Boston
    -- Boston Night Life
    -- Boston Restaurants
    -- Boston Hotels

    Is there a way to do this so that the page shows the articles within boston but also shows them defined within their subcategory list so.

    (root category)Boston
    -- (sub category)Boston Night Life
    ---- (article) Boston night life is the hottest party town
    ---- (article) Boston knows how to party

    -- (sub category) Boston Restaurants
    ---- (article) Boston Restaurants are the best
    ---- (article) Do you know where the Best Boston spots for Lunch?

    =====

    Any help would be amazing.

    Thank you.

  12. stowepro
    Member
    Posted 3 years ago #

    This is how I setup your code... It did work but not any more and im scratching my head over it..

    <div>
    
     <?php
    
    	query_posts(array('category__in' => array($cat),'paged' => $paged));
    
    ?>
    
             <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
        <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" >
     		<?php the_title(); ?>
        </a> <!-- #=service-box END -->
    <?php endwhile; endif; ?>
    </div>
    
        <?php wp_reset_query(); ?>

    When it was working it would show the articles within the boston category under the Additional Articles on a page like this..
    http://jetsetlife.tv/destinations/boston/boston-nightlife/

    Again any help would be amazing. and if the above question could be done as well would perfect.

    Thank you.

  13. Jennifer
    Member
    Posted 3 years ago #

    oh no, if it broke, then ill need a fix as well. the above code that i posted works for me on 3.1, but i have not upgraded to 3.1.1 yet. hope you'll post your fix to this thread if you figure it out before i do :) thanks!

  14. stowepro
    Member
    Posted 3 years ago #

    I'm trying anything I can think of and if something hit and works, I'll post.

  15. stowepro
    Member
    Posted 3 years ago #

    found some code that kind of works...

    This is what it looks like...

    <?php
                  foreach( ( get_the_category() ) as $category ) {
                  $the_query = new WP_Query('category_name=' . $category->category_nicename . '&showposts=10');
    while ($the_query->have_posts()) : $the_query->the_post();
          ?>
    
         <li>
             <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" target="_parent" >
     		<?php the_title(); ?>
              </a> <!-- #= -->
         </li>
    
    <?php endwhile; ?>
    <?php
         }
    ?>

    It's not perfect yet but its something..

  16. osmir
    Member
    Posted 2 years ago #

    I got my solution of the topic.
    Main idea is to filter out posts placed in subcategories. So we can use negative values in cat variable of the query_posts().

    My steps:

    1. Determinig list of subcategories for current category and merging them together
      $subcategories = get_categories(array('type' => 'post', 'child_of' => $_GET['cat']));
      
      foreach ($subcategories as $i => $value) {
          $excluded_cats .= ",-".$value->cat_ID;
      }

      You've got something like ,-10,-5,-3 Note leading comma.

    2. Generating cat variable
      if (!empty($excluded_cats)) {
         $MainLoopArgs = array('cat' => $_GET['cat'] . $excluded_cats);
      }
      else {
         $MainLoopArgs = array('cat' => $_GET['cat']);
      }

      I include current category and exclude subcategories in one cat. Result is something like 4,-10,-5,-3.

    3. Passing $MainLoopArgs into query_posts()
      query_posts( $MainLoopArgs );

    Sorry, maybe my example not so graceful, but it works! :)

    P.S. Yes, you have to make (m)any validation checks of variables for realy robust app.

Topic Closed

This topic has been closed to new replies.

About this Topic