Home Forums General Issues Image field returns ID, not Array, when filtering


Image field returns ID, not Array, when filtering

  • I am filtering my custom post type (with custom fields) using this method:

    The only problem is that when I filter the data the Image field returns the ID, not the Array.

    I could change the return type to always be the Image ID, but I prefer the Image Array and would like to see if I am doing something wrong. I’ve set this up as a test on a site with very few plugins to ensure it’s not caused by a conflict.

    My code follows the simple first example in the video linked above:

    add_action('pre_get_posts', 'my_pre_get_posts');
    function my_pre_get_posts( $query ) {
    	if( is_admin() ) {
        $name = 'advisory_board';
        if(empty($_GET[ $name ]) || $_GET[$name] != '1') {
    $meta_query = $query->get('meta_query');
    $meta_query[] = array(
            'key'		=> 'person_advisory_board',
            'value'		=> '1',
            'compare'	=> '='
    $query->set('meta_query', $meta_query);

    Thanks for any help with this.

  • Do you mean that an image field used somewhere else is returning the image ID instead of an array? I don’t see where you’re trying to get an image in your code and I’m just what to understand what the problem is.

  • You are correct, I am trying to get an image in my template for this custom post type. When I don’t filter the data with the above code I receive an array, when I do filter it, I receive an int. Here is the code I use in my template file:

    $image = get_field('my_image');
    	if(!empty($image)) {
    			echo'This is a number: '.$image.'<br />';
    		else {
    			$photo = '<img width="200" src="'.$image['url'].'" alt="'.$image['alt'].'" /><br /><br />';
    			echo $photo;

    Thanks for your quick reply.

  • Well, the pre_get_posts filter you have is probably interfering with other queries. What you need to do is to limit it more.

    Instead of just checking to see if it’s the admin do something like

    if (is_admin() || !$query->is_main_query()) {
      // limit to only the main query

    You may also need to do further limiting, for example limiting it to a specific post type

    if (!isset($query->query_vars['post_type']) || 
        $query->query_vars['post_type'] != 'post') {

    the code examples given here are meant for reference and simple examples and may not be everything you need.

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

The topic ‘Image field returns ID, not Array, when filtering’ is closed to new replies.