• 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; ?>
Viewing 15 replies - 1 through 15 (of 15 total)
  • Thread Starter Jennifer

    (@jks7152)

    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!

    Thread Starter Jennifer

    (@jks7152)

    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.

    Thread Starter Jennifer

    (@jks7152)

    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();
    
    ?>
    Thread Starter Jennifer

    (@jks7152)

    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();
    
    ?>
    Thread Starter Jennifer

    (@jks7152)

    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.

    Thread Starter Jennifer

    (@jks7152)

    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));
    
    ?>

    Thanks for the tip. πŸ™‚

    Thanks!

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

    stowepro

    (@stowepro)

    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.

    stowepro

    (@stowepro)

    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.

    Thread Starter Jennifer

    (@jks7152)

    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!

    stowepro

    (@stowepro)

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

    stowepro

    (@stowepro)

    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..

    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.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Get Parent Category Posts, Not Sub-Categories Posts Unless Belong to Parent Also’ is closed to new replies.