Support

Account

Home Forums ACF PRO ACF and pre_get_posts

Helping

ACF and pre_get_posts

  • Hello
    I’ve a custom archive page showing some ACF field values – if I add the following piece of code, all the ACF fields are blank – nothing is shown on the archive page – I’d like to show only posts on that archive page where the status field is set to ‘active’ – what am I missing.
    thank you in advance for you input.

    add_action( 'pre_get_posts', 'themeprefix_pre_get_posts' );
    function themeprefix_pre_get_posts( $query ) {
    	
    	if ( is_admin() )
    		return $query;
    	
    	// Show only active job openings
    	if ( is_post_type_archive ( 'job' ) ) {
    		$query->set( 'meta_key',   'status' );
    		$query->set( 'meta_value', 'active' );
    	}
    	
    	return $query;
    }
  • You need to do some additional checking

    
    add_action( 'pre_get_posts', 'themeprefix_pre_get_posts' );
    function themeprefix_pre_get_posts( $query ) {
    	
    	if ( is_admin() || !$query->is_main_query() )
    		return $query;
    	
    	// Show only active job openings
    	if ( is_post_type_archive ( 'job' ) ) {
    		$query->set( 'meta_key',   'status' );
    		$query->set( 'meta_value', 'active' );
    	}
    	
    	return $query;
    }
    

    there may also be some additional checks you need to do.

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

The topic ‘ACF and pre_get_posts’ is closed to new replies.