Site logo

Archived topic

How to have posts not appearing on homepage?

12 replies · Started by Philippe on April 26, 2021

Viewing posts 1–13 of 13

Hi,

Even though I leave the "Stick to the top of the blog" box unchecked, my most recent post appears on the homepage. I do not want that. It's been a while I worked on my website and have forgotten what to do to have specific posts not appearing on the homepage.

Thanks for your help,

Philippe

Hi there,

can you share a link to your site so i can see what we're dealing with

Sure: http://www.onehomeplanet.com

I have 10 posts or so published this far, and only 3 appearing on the homepage. I would like to be able to keep that distinction between posts appearing on the homepage and posts that must not.

Theres a few ways to do this.
You could try setting the Stick to Top for the posts you want displayed.

And then add this PHP Snippet to filter the post loop so it only displays sticky posts:

add_action('pre_get_posts', 'WPSE_home_only_stickies');
function WPSE_home_only_stickies($query) {
    // only run for homepage & main query
    if ($query->is_home() && $query->is_main_query()) {
        $sticky_posts = get_option('sticky_posts');
        $query->set('post__in', $sticky_posts);
    }
}

For reference the snippet originated here: https://wordpress.stackexchange.com/a/315316
And i just quickly tested it and it worked.

Well, good news.

I recreated the post and gave it a new title/slug (it had the same title as a page (not post) relating to the same topic). As I did not check the box to make it sticky... it does not stick.

Could it be that when a post has the same title as page, it remains sticky no matter what?

IT is full of mysteries. :)

I had not seen your previous answer. Thank you.

Did that method i provided work ?

I did not need to use it. See my previous answer.

Hi Philippe,

I don't understand your replies, have you tried David's method?

The method David provided should only work on posts, not pages.

Hi Ying,

The was fixed before I read David's answer.

Ah ok, that's good!

Hi,

The same issue reappeared today.

I went into functions.php, just to discover that it already contained the snippet below. I just had to update the category number id to make things work the way I want.

Happy

add_action( 'wp_enqueue_scripts', 'generatepress_child_enqueue_scripts', 100 );
add_filter('pre_get_posts', 'excludeCat');
function excludeCat($query) {
if ( $query->is_home ) {
$query->set('cat', '-35');
}
return $query;
}

So what's the question here? It's all good?

This archived topic is closed to new replies.