Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #1446898

    Hello there, I’m currently using a filter like this to display sticky posts in a blog grid.
    This affects all blog grids on the site.
    We have many blog grids and only want these sticky posts to display on the home page.
    Is there a way to do this?
    Thank you!

    • This topic was modified 5 months ago by NCATIT.
    • This topic was modified 5 months ago by NCATIT.
    #1446903

    Hey NCATIT,

    Thank you for the inquiry.

    You can add a condition at the very top of the avia_post_slide_query_mod function to filter out other pages. Example:

    if(!is_home() || !is_front_page()) return $query;
    

    Best regards,
    Ismael

    #1446943

    Worked a treat! Thank you Ismael!
    NCATIT

    #1446968

    Hi,

    Great, I’m glad that Ismael could help you out. Please let us know if you should need any further help on the topic, or if we can close it.

    Best regards,
    Rikard

    #1461726

    Hello Rikard, I hope all is well.
    Upon closer inspection I noticed the sticky posts are showing up on the category pages. i.e. /category/blog/
    Is there a way to suppress them from showing at the top of category pages?
    Thank you for any advice.
    NCATIT

    #1461754

    Hi,

    Thank you for the update.

    Please try this code in the functions.php file:

    function av_exclude_sticky_posts_from_category($query) {
        if ($query->is_category()) {
            $query->set('ignore_sticky_posts', true);
        }
    }
    add_action('pre_get_posts', 'av_exclude_sticky_posts_from_category');
    

    Best regards,
    Ismael

    #1461788

    Darn that doesn’t seem to work. I think it might have something to do with taxonomy becasue the url I’m trying to suppress sticky posts on looks like this:
    /wp-admin/term.php?taxonomy=category&tag_ID=53&post_type=post&wp_http_referer=/wp-admin/edit-tags.php?taxonomy=category
    Any ideas? Thank you!

    #1461814

    Hi,

    Thank you for the update.

    Are you trying to suppress the sticky posts on the backend? Category is a type of taxonomy, so the changes above should affect the category pages. We may need to check the actual category page and access the dashboard to properly check the modification. Please provide the login details in the private field.

    Best regards,
    Ismael

    #1462102

    Hello Ismael, any updates on this? Thank you. :-)

    #1462134

    Hi,

    In our previous message, we requested login credentials to access the dashboard. Please provide the details in the private field.

    Best regards,
    Ismael

    #1462166
    This reply has been marked as private.
    #1462221

    Hi,

    Thank you for the info.

    We edited the functions.php file and adjusted the filter to keep the sticky posts on the front page and disable them on the category pages.

    function exclude_sticky_posts_from_category_pages_filter($query) {
        if (is_admin()) {
            return $query;
        }
    
        if (is_front_page()) {
            $include = array();
            $sticky = get_option('sticky_posts');
            $args = array(
                'post__not_in' => $sticky,
                'category__in' => array(4, 31, 51, 52),
            );
    
            $posts = get_posts($args);
    
            foreach ($posts as $post) {
                $include[] = $post->ID;
            }
    
            $include = array_merge($sticky, $include);
            $include = array_map('intval', $include);
    
            $query['post__in'] = $include;
            $query['orderby'] = 'post__in';
            return $query;
        }
    
        if (is_category()) {
            $sticky_posts = get_option('sticky_posts');
    
            if (!empty($sticky_posts)) {
                $query['post__not_in'] = $sticky_posts;
            }
        }
    
        return $query;
    }
    
    add_filter('avia_post_slide_query', 'exclude_sticky_posts_from_category_pages_filter');
    
    

    Please make sure to delete the cache before checking the pages.

    Best regards,
    Ismael

    #1462306

    Thank you so much Ismael. I really appreciate it. :-)

    #1462344

    Hi,

    No problem! Glad we could be of help. Please feel free to open another thread should you have more questions about the theme.

    Have a nice day.

    Best regards,
    Ismael

Viewing 14 posts - 1 through 14 (of 14 total)
  • The topic ‘Sticky posts display on home page only’ is closed to new replies.