Support

Account

Home Forums Bug Reports get_field image returns ID only on search results page (PRO)

Solving

get_field image returns ID only on search results page (PRO)

  • Recently switched to ACF PRO and noticed that get_image does not return an array, but rather the attachment ID, only on the search results page. The return value is set to “Image Array”. I tried switching back to ACF 4 and everything works fine.

    On all other pages get_field image returns the array.

  • Did you ever find a solution to this? I’m having the exact same issue.

  • Hi @metropolis

    get_image is not an ACF function (or WP function). Do you mean that using get_field on an image field has this result?

  • Sorry to revive this topic but having same issue using

    get_field('training_header_image','option');

    and no matter what return value setting i have, it is always returning the ID, made a vardump and got this:

    string(3) "724"

    this happens only in the search results page for wordpress, and it is the only field its not working, cause i have other fields for some custom titles and they are displaying fine.

    Used the same code on another section of the same page and it works, the issue is in the search.php file of wordpress where its presenting the bug.

    im running lastest version 5.5.7 with the “get_field()” fix, this is the first time i have problems with ACF

    please help.

  • @chewienator i had the same issue today and i’ve figured out that i have to modify pre_get_posts action that i had declared.

    function mine_add_custom_types( $query ) {
      if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
        $query->set( 'post_type', array(
         'post', 'nav_menu_item', 'quiz', 'acf-field', 'acf-field-group', 'listicle'
    		));
    	  return $query;
    	}
    }
    add_filter( 'pre_get_posts', 'mine_add_custom_types' );

    as you see i had to add post_types: ‘acf-field’, ‘acf-field-group’ to pull proper options values.

  • Whenever this happens, 99% of the time is has to do with a pre_get_posts filter filtering something it shouldn’t. All of the examples that you’ll find, or at least most of them that I’ve seen, do not take into account that the filter is run on every query done in WP and the examples to not include information on making sure you don’t apply the filter to things that you should not. The reason that the examples don’t is because, well, that information is not easy to come by and takes a bit of trial and error.

    This is where the PHP error log comes in handy.

    
    function mine_add_custom_types( $query ) {
      ob_start();
      print_r($query);
      error_log(ob_get_clean());
    }
    add_filter( 'pre_get_posts', 'mine_add_custom_types' );
    

    This will put every query done when loading a page into the PHP (WP) error log. You can then examine the error logs and figure out how to target only the query you want to alter.

    Although, in the case of the search results and adding post types is to make sure you only apply your filter to “The Main Query”

    
    function mine_add_custom_types( $query ) {
      if (!$query->is_main_query()) {
        return;
      }
      // the rest of your code
    }
    add_filter( 'pre_get_posts', 'mine_add_custom_types' );
    
  • @zychu69 hey thanks! i would have not guessed that we would have to add the post types from ACF.

    i removed the implementation in functions, cause when i added the ACF fields they would get returned within the search results.

    i just needed to supress page post_type from search

    found this that helped me if anyone is interested:

    /**
     * update_my_custom_type
     *
     * @author  Joe Sexton <joe@webtipblog.com>
     */
    function update_my_custom_type() {
    	global $wp_post_types;
     
    	if ( post_type_exists( 'page' ) ) {
     
    		// exclude from search results
    		$wp_post_types['page']->exclude_from_search = true;
    	}
    }
  • Hey I had the same error; to fix it I am getting the id and later the url of that id:

    $url = wp_get_attachment_image_src( get_field('the_name_of_my_field', 'option') );
    echo $url[0];

    Maybe this is not the best practice but works for me.

  • @jjot93 and others, one of the reasons I rarely see this issue is that I almost never return anything from an image field but the ID anyway. The reason is that when there are many images on a page, and when you have several custom image sized set up in WP, returning an array of all of the images causes a lot of unnecessary work to be done getting all of the data for all of those different image sizes and this can slow things down. I find it better to get the image ID and then just do the work myself.

  • @john Huebner; hey you are right, “I have deleted my filters from functions.php later added the plugin Search Everything”. This helped my a lot, ACF is working as always, thank you very much for all the help.

  • This can also happen if you call get_field too early, I had a case where I called it at the top of the functions.php file in a $_GET check and it wouldn’t load the data for a repeater field, just the number of elements.
    For Example:

    
    if( $_GET['dostuff'] == 'yes'){
      $data = get_field('fieldname', 'option');
      //do stuff
      //$data returned "3"
    }

    I solved this by moving it to a function in an init action

    
    if( $_GET['dostuff'] == 'yes'){
    add_action('init', 'functionname');
    }
    function functionname(){
      $data = get_field('fieldname', 'option');
      //do stuff
      //$data returned array
    }
    
  • This works, Thanks

    function mine_add_custom_types( $query ) {
      if (!$query->is_main_query()) {
        return;
      }
      // the rest of your code
    }
    add_filter( 'pre_get_posts', 'mine_add_custom_types' );
Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.