Support

Account

Home Forums Bug Reports get_field() from WP_Term returns ID on archive page

Solved

get_field() from WP_Term returns ID on archive page

  • Hello!

    I’m trying to access a custom image field on categories. On regular pages, it works perfectly.

    I simply do:

    $cat_args = array(
      'taxonomy' => 'category',
      'exclude'  => 1,
      'hide_empty' => true
    );
    $categories = get_terms( $cat_args );
    
    foreach ($categories as $category) :
      $poster = get_field('category_bg', $category);
      echo $poster ? '<img class="visuallyhidden" src="' . $poster['sizes']['big'] . '" alt="" />' : '';
    endforeach;

    on the pages, $poster is an object while on the archive page, $poster is a string containing ID :/
    More precisions : it’s a custom post archive. Custom Post Type : oeuvre

    Thanks 🙂

  • 90% of the time when this happens it is due to a conflict with something. It could be a another plugin in something in your theme. More often then not it has been a pre_get_posts filter that is interfering with the acf queries.

    You need to try to narrow down what it is by deactivating plugins and maybe switching themes.

    If you do a search in this forum for “image field returns id instead of array” the topics that come up may help you.

  • Hello @hube2 !

    Thank you for your message !

    You were right, it was a conflict with a filter function that i’ve added to show Custom Post Types in archives. Otherwise, only posts are displayed :/

    This one exactly:

    
    /**
     * Archives.php only shows content of type 'post', but you can alter it to include custom post types.
     * Add this filter to your functions.php file
     * @url https://css-tricks.com/snippets/wordpress/make-archives-php-include-custom-post-types/
     */
    function proov_add_custom_types( $query ) {
      if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
        $query->set( 'post_type', array(
         'post', 'nav_menu_item', 'oeuvre'
    		));
    	  return $query;
    	}
    }
    add_filter( 'pre_get_posts', 'proov_add_custom_types' );
    

    I’m gonna try something else ^^

  • 
    function proov_add_custom_types( $query ) {
      // add condition to make sure it's the the main query
      if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] )
          && $query->is_main_query() ) {
        $query->set( 'post_type', array(
         'post', 'nav_menu_item', 'oeuvre'
    		));
    	  return $query;
    	}
    }
    add_filter( 'pre_get_posts', 'proov_add_custom_types' );
    
  • I found a solution on the Codex 🙂

    This works 🙂

    
      /**
      * Registering Post Types
      */
      // Show posts of 'post', 'page' and 'oeuvre' post types on category page
      add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
      
      function add_my_post_types_to_query( $query ) {
        if ( is_category() && $query->is_main_query() )
        $query->set( 'post_type', array(
          'post',
          'page',
          'oeuvre'
        ));
        return $query;
      }
    
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘get_field() from WP_Term returns ID on archive page’ is closed to new replies.