Archived topic
How to Hide Individual Posts from Site
12 replies · Started by Kelsey on March 2, 2022
Hi,
I'd like my posts to still be indexed and found, but I don't want any links to those posts to appear on my site. I don't want them to show up as new posts, etc. How can I accomplish this? Here's one of the pages in question:
https://thegoldenlamb.com/how-to-run-an-seo-report-in-rankiq/
Since my blog is a recipe blog, I don't want this software review blog post to show up to my normal audience.
Thanks,
Hi there,
if you want to exclude specific posts from the main query then you can:
1. Create a category term that you will use to exclude the posts eg. no-show
Edit that category term and get its ID from the Browser URL Field, you will need this.
2. Assign all the posts you want hidden from the feed with the no-show term
3. Add this PHP Snippet:
add_action('pre_get_posts', 'exclude_category_posts');
function exclude_category_posts( $query ) {
if($query->is_main_query() && !is_admin()) {
$query->set('cat', array( -22 ));
}
}
In this part of the code:
$query->set('cat', array( -22 ));
You need to change the -22 to minus your term ID - so if the ID=66 then the line will be:
$query->set('cat', array( -66 ));
This will exclude those posts from the main query.
What Google will make of this, i have no idea :)
Thanks, David. Where do I place the final PHP code? In my functions.php file? Would want to include it a child version, no?
Hi Kelsey,
You can add the PHP code using one of the method it introduced:
https://docs.generatepress.com/article/adding-php/
Please make sure you have a child theme installed if you want to add it to functions.php file.
https://docs.generatepress.com/article/using-child-theme/
Thanks Ying,
Is there a way to make this work on Mobile? Your fix works on desktop but I need it to also work on mobile.
Thanks,
Kelsey
Hi Kelsey,
The Post “How to Run an SEO Report in RankIQ” still appears on my end on both Desktop and mobile.
Moreover, the code provided by David should work on any view/device. Can you confirm if you’ve added the correct Category ID?
Kindly see: https://share.getcloudapp.com/6quZKD4D
Hope to hear from you soon. :)
Hi there,
I added this code:
add_action('pre_get_posts', 'exclude_category_posts');
function exclude_category_posts( $query ) {
if($query->is_main_query() && !is_admin()) {
$query->set('cat', array( -403 ));
}
}
with my post ID. I used a snippets plugin to add it. I would add it to my functions.php file but I don't seem to have a child theme for GeneratePress? I add all my custom CSS within the "customize" function on my dashboard rather than editing my actual CSS file.
Do I literally just paste the above code at the end of my functions.php file? I don't want to break my site, lol. I did once before, years ago, when adding code to functions.php so I'm a bit frightened about doing it again.
Not post ID, category ID. That was a typo.
If you don't have a child theme then you need to install the Code Snippets plugin:
https://en-ca.wordpress.org/plugins/code-snippets/
Add a new Snippet and paste that code in.
The code is device agnostic - you may just need to clear any server/plugin/browser caches to see the change on your mobile. If the issue persists connect to a different network and test
Hi David,
Thank you. I tried it again. I think it worked. Can you check on your end?
https://thegoldenlamb.com/how-to-run-an-seo-report-in-rankiq/
Thank you
That post displays correctly, and i cannot find it in the main loop :) All good to me.
Great. Thank you.
You're welcome