• chinarose

    (@chinarose)


    On the footer of the homepage, I wrote a query to pull in recent posts categorized as ‘news’ and only show 4.

    What I would like to do now is pull back posts categorized as ‘news’ and show those marked as sticky before the others.

    Here is what I have. Can you tell me how I could change it to pull in sticky posts above those that are not marked as sticky?

    function get_recent_posts_front(){
    	
    	$args = array(
    		'post_type'		=> 'post',
    		'posts_per_page' 	=> 4,
    		'orderby'		=> 'date',
    		'order'			=> 'DESC',
    		'tax_query' => array(
            array(
                'taxonomy' => 'category',
                'field'    => 'slug',
                'terms'    => 'news'
            )
        ));
    	
    	$query 	= new WP_Query($args);
    	
    	if(!$query->have_posts()) return false;
    	
    	$cards 	= array();
    	
    	while($query->have_posts()): $query->the_post();
    	
    		$cards[] = get_media_card($query->post);
    	
    	endwhile;
    	
    	return $cards;
    	
    }

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    You can get sticky post IDs from the “sticky_posts” option. Get the sticky post objects though a separate query using “post__in” arg and place the results in the $cards array before doing so with the main query results.

    One would think setting the “ignore_sticky_posts” arg to false would include stickies in the main query but it doesn’t seem to have any effect on queries involving “tax_query”.

    Thread Starter chinarose

    (@chinarose)

    Hello,

    Thank you for the direction, I appreciate it!

    I was able to get sticky posts showing in that area of the homepage which was great. The trouble I ran into is for the scenario where something is sticky and news as well, that post would be pulled in twice.

    If anyone has insights about how to resolve the issue of duplicate posts, please let me know.

    Thank you,
    China

    Moderator bcworkz

    (@bcworkz)

    Ah yes, that is an issue. The usual approach is to take the array of post IDs from the sticky_posts option and use it in a “post__not_in” arg for the news items query.

    If that should not work for some reason, the other approach is in your original loop, wrap the normal news post assignment to $cards in a “if current post ID is not in sticky array” logic structure that suppresses assignment should the post be a sticky post.

    Thread Starter chinarose

    (@chinarose)

    I appreciate the feedback, thank you. I will need to think about how to do this. If/when I get a solution, I will post back here.

    My short term solution, which is a bit of a hack, is to change the publication date of the post that should appear first in the news category. But I hope to somehow figure out how to naturally bring that in with a modified query.

    Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Show sticky posts first in call to WP_Query for a specific category’ is closed to new replies.