• I’m attempting to grab the top 3 stickys in a specific category within functions.php.

    I made it work as long as there are no sub categories with sticky posts. If there are, it breaks.

    Is there any way to grab the stickies from a parent and it’s subcategories?

    This is the functioning code.

    It works on this page: https://www.machtest.com/category/trailblazers/
    But once I have multiple subcategories with stickies from those categories, it displays none: https://www.machtest.com/category/jus-sayin/

    function popular_posts_per_category() {
    	global $post;
    	$categories = get_the_category();
    	foreach($categories as $category) {
    		$cats[] = $category->cat_ID;
    	}
    
    	$cats_unique = array_unique($cats);
    	$sticky = get_option('sticky_posts');
    	$args = array(
    		'category__in' => $cats_unique,
    		'orderby' => 'date',
    		'order' => 'ASC',
    		'post_type' => 'post',
    		'post__in'  =>  $sticky,
    		'post_status' => 'publish',
    		'posts_per_page' => 3
    	);
    foreach((get_the_category()) as $category) {
     echo '<h2>Featured Posts</h2>';
     }
    	$popular_posts = null;
    	$popular_posts = new WP_Query($args);
    	echo '<div style="padding-bottom: 20px; margin-bottom: 35px; border-bottom: 1px dashed #333;">';
    	while ($popular_posts->have_posts()) : $popular_posts->the_post();
    		$title_trim = get_the_title();
    		if (strlen($title_trim) > 60) {
    			$title_trim = substr($title_trim,0,60);
    		} ?>
    
    		<div style="float: left; margin-right: 20px; width: 250px; text-align:center;"><a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail($page->ID, 'port2'); ?></a><a href="<?php the_permalink(); ?>"><h4 style="margin-bottom:0;"><?php echo $title_trim; ?></a></h4><?php echo date("F j, Y"); ?></div>
    	<?php endwhile;
    	rewind_posts();
    	echo '<div style="clear: both; height: 0; overflow: hidden;"></div></div>';
    }
Viewing 1 replies (of 1 total)
  • Thread Starter heyepic

    (@heyepic)

    I’m echoing the category it’s grabbing. It seems to grab the subcategory ID that has the most recent post. If there are no subcategories, it echos the parent category ID.

Viewing 1 replies (of 1 total)
  • The topic ‘Retrieve 3 Sticky Posts from Parent and Sub Categories’ is closed to new replies.