/* ================================================
HIDE BLOG UPDATES FROM HOMEPAGE UNLESS PINNED
================================================ */

add_action(‘pre_get_posts’, function($query) {
if ($query->is_home() && $query->is_main_query()) {
// Get sticky posts
$sticky_posts = get_option(‘sticky_posts’);

    // Get Blog Updates category ID
    $blog_updates_cat = get_cat_ID('blog-updates');

    if ($blog_updates_cat) {
        // Exclude Blog Updates category
        $query->set('category__not_in', array($blog_updates_cat));

        // But include sticky posts from Blog Updates
        if (!empty($sticky_posts)) {
            $query->set('post__not_in', array());

            // Get non-sticky Blog Updates posts to exclude
            $blog_update_posts = get_posts(array(
                'category' => $blog_updates_cat,
                'numberposts' => -1,
                'fields' => 'ids',
                'exclude' => $sticky_posts
            ));

            if (!empty($blog_update_posts)) {
                $query->set('post__not_in', $blog_update_posts);
                $query->set('category__not_in', array());
            }
        }
    }
}

});

Subscribe
Notify of
guest
0 Comments