Editing posts on the home page

Mauer Themes Support Storyteller Editing posts on the home page

This topic contains 1 reply, has 2 voices, and was last updated by  Paul 11 months, 3 weeks ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1711

    Haggblon
    Participant

    Is there a way to exclude certain posts to be viewed on the home page – posts in a certain category for instance?

    #1718

    Paul
    Keymaster

    Nina-Maria,

    You can alter the set of the posts that is being displayed on the front page by using WordPress’s pre_get_posts filter. For instance, to exclude posts belonging to a certain category, you can use the following code:

    function mauer_filter_posts_on_frontpage($query) 
    {
    		if ($query->is_main_query() && is_front_page()) {
    				$categories_to_exclude = array(2, 6); // IDs of the categories to exclude
    				$query->set('category__not_in', $categories_to_exclude);
    		}
    		return $query;
    }
    add_action('pre_get_posts', 'mauer_filter_posts_on_frontpage');

    This code should be placed in functions.php of your theme (for example at the end of it, before the closing ?>, like so). Or in a child theme / plugin, which would save it from being overwritten when you update the theme.

    The example above will exclude the categories with id 2 and id 6. Feel free to substitute these numbers with the IDs you want to exclude. If you want to exclude one category, just delete the second number and the comma, so that it looks like this array(2).

    You can find the IDs of your categories in your Admin Panel by going to ‘Posts’ -> ‘Categories’, and then clicking a category name. The category ID will be in the URL of the page after the ‘tag_ID=’. For instance, in this screenshot the ID of the category being viewed is 15 (see the highlighted part of the page address).

    Regards,
    Paul

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.