• Im kind of making my own sidebar and I have a unique design for each category i list (not all) and i would like to somehow retrieve the newsest post from that category. I havent been able to find any easy way to do this without just querying the db directly.

    To make things a little more complicated. I have a category with subcategories and i want the newest post no matter what subcategory its in. I still want to list its subcategory name though.

    Any ideas/suggestions? Ive only been using WP for 2 days now, so I have a lot to learn.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Here is the code to use. Just make sure you put your actual category number instead of CATNUMBER in the code below. You can find your category ID in the category section of the admin.

    <?php $posts = get_posts( "category=CATNUMBER&numberposts=1" ); ?>
    <?php if( $posts ) : ?>
    <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
    
    <div class="post">
    DO YOUR THING HERE
    </div>
    
    <?php endforeach; ?>
    <?php endif; ?>

    If you use the main category it will automatically include all of the sub categories. Let me know if you have any issues.

    Thread Starter MACscr

    (@macscr)

    sweet, thank you so much!! Will give it a try right away.

    Thread Starter MACscr

    (@macscr)

    Works perfect except for the issue with subcategories. I have a category called “Character Blogs”, then each Character has his own subcategory, like “Jason Smith” or whatever. The problem is that i want to list a new post that falls under “Character”, no matter what subcategory its in. Any ideas on this? I really appreciated your initial help as well.

    Thread Starter MACscr

    (@macscr)

    shoot, looks like i cant create permalinks from those results as well. Hmmm.

    Thread Starter MACscr

    (@macscr)

    ok, this is dirty, but its gets a list of child category ids and then comma seperates them so they can be used in the above function.

    function list_childcat_ids($id) {
    
    	$query = "SELECT cat_ID FROM wp_categories WHERE category_parent=".$id.""; 
    
    	$result = mysql_query($query) or die(mysql_error());
    
    	while ($array = mysql_fetch_array($result))	{
    		$data[] = $array[0];
    	}	
    
    	$cat_id_list = implode(",", $data);
    
    	return $cat_id_list;
    }

    Now i just have to figure out how to get the proper urls.

    Thread Starter MACscr

    (@macscr)

    ok, got everything working now. Here example code for what i used to display the newest post from any subcategory from a particular parent category. (lol, if that makes sense)

    <?php $catid = list_childcat_ids('3'); ?>
    <?php $posts = get_posts( "category=".$catid."&numberposts=1" ); ?>
    <?php if( $posts ) : ?>
    <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
    
    <div class="post">
    <a href="<?php echo get_permalink($post->ID); ?>" ><?php echo $post->post_title; ?></a>
    </div>
    
    <?php endforeach; ?>
    <?php endif; ?>

    Wow…. you learned a lot fast! Good job!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Get Newest post for Category’ is closed to new replies.