Support

Account

Home Forums General Issues Image field on term returning ID instead of Array

Solved

Image field on term returning ID instead of Array

  • Hi,

    I have an image field on category terms with the output set to array. For some reason in my category.php it returns the ID of the image however. In other template files like index.php or single.php it does return the array as expected.

    Putting the exact same code, for example get_field('image', 'category_2'), gives me different outputs…

    Am I missing something, or a bug?

    Thanks, Alain

    (WP 4.4.1 / ACF 5.3.3.1)

  • Ok found it… I was messing around with the query for categories (making it return my custom post type: course).

    Had this in my functions:

    function alter_category_query($query) {
    	if(is_category() && empty($query->query_vars['suppress_filters'])) {
    		$query->set('post_type', array('course'));
    		return $query;
    	}
    }
    add_filter('pre_get_posts', 'alter_category_query');

    changed it to also include the acf-field post type:
    $query->set('post_type', array('course', 'acf-field'));

  • Thx !
    I had exactly the same problem.

  • Tnx. from me too! Similar problem here but I was adding CPTs to search results.

  • It’s extremely difficult to eliminate all the possibilities like in the original post.

    For example, adding additional post type to a site would mean having additional problems in the future. Or returning posts that you don’t want from a post type you don’t really want for search results.

    A better solution would be to make sure that you are currently querying a post type that can have ‘category’ as a term.

    
    function alter_category_query($query) {
    	if(is_category() && empty($query->query_vars['suppress_filters']) 
               && empty($query->query_vars['suppress_filters']) ||
               $query->query_vars['suppress_filters'] == 'post') {
    		$query->set('post_type', array('course'));
    		return $query;
    	}
    }
    add_filter('pre_get_posts', 'alter_category_query');
    

    The above will only make the change if you’re currently doing a query for posts and you won’t need to worry about future changes causing problems you’ll need to track down.

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

The topic ‘Image field on term returning ID instead of Array’ is closed to new replies.