Support

Account

Home Forums ACF PRO Remove Featured Post From Main Blog Loop Using ACF Field

Solved

Remove Featured Post From Main Blog Loop Using ACF Field

  • So I’m simply using a post object field on the Posts Page to choose a featured post. I then need to take the ID from that field and use it in functions.php to filter out that featured post from the blog loop.

    For some reason I can’t get this to work at all. When I manually enter in the ID, the post is correctly remove – however I can’t get it to dynamically work. I’ve tried use global variables set in the archive page and call it in the functions page, and I’ve tried just manually calling the field in functions itself.

    I would appreciate *any* help for this. Thanks in advance!

    Here is the current iteration of my code:

    $featuredPost = get_field('choose_featured_post',29);
    $featuredPostID = $featuredPost->ID;
    
    add_action( 'pre_get_posts', 'wpsites_remove_posts_from_home_page' );
    function wpsites_remove_posts_from_home_page( $query ) {
    
        if( $query->is_main_query() && $query->is_home() ) {
    	    
            $query->set( 'post__not_in', array( $featuredPostID ) );
            
        }
    }
  • Got it to work! Added global $featuredPostID inside the function 🙂

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

You must be logged in to reply to this topic.