Support

Account

Home Forums Front-end Issues Array WP_Error trying to get ACF taxonomy value

Solved

Array WP_Error trying to get ACF taxonomy value

    • udapud

    • July 11, 2013 at 11:59 pm

    Trying to add an extra text field to a taxonomy called Brands (product_brand) and getting this error with code on fresh install.

    
    Fatal error: Cannot use object of type WP_Error as array in /home/mjackson/public_html/kinglouie.me/wp-content/themes/superstore/woocommerce/archive-brand.php on line 46
    

    original code (line 46 is $term = $terms[0];)

    
    <?php
     
    global $post;
     
    // load all 'category' terms for the post
    $terms = get_the_terms($post->ID, 'brand');
     
    // we will use the first term to load ACF data from
    if( !empty($terms) )
    {
    	$term = $terms[0];
     
    	$custom_field = get_field('contact_info', 'category_' . $term->term_id );
     
    	// do something with $custom_field
    }
     
    ?>
    
    • udapud

    • July 12, 2013 at 12:10 am

    Your help documents say that to Display a field in my Archive template all I need do is include this code

    
    <p><?php the_field('field_name', 'category_7'); ?></p>
    

    With my relevant data. However I can’t seem to get this to work either. Is it possible you could perhaps include a code snippet example for every setup that works on fresh, unmodified installs? Whether it be for adding WYSIWYG’s or Calender or, like in my case, a simple text field. This would certainly help me a lot just now and I suspect a few more out there.

    Great looking new forum btw 🙂

  • @udapud,

    The first issue is not ACF related (its throwing an error before getting to the ACF function). If your taxonomy slug is ‘product_brand’, your get_the_terms() function should be

     $terms = get_the_terms($post->ID, "product_brand");
    

    In addition to checking if the term array is empty, you should check for a WP error like:

     if ( $terms && ! $terms->is_wp_error() ) { ... }
    

    Since WP_Error is an object, it can’t be used as an array like in $term[0].

    The second code posted should work on a fresh install, provided you have a field called “field_name” (with a value) for taxonomy “Category” for the term with ID 7.

    • Elliot

    • July 12, 2013 at 4:51 pm

    Hi @udapud

    Instead of using


    $term = $terms[0];

    Can you try

    
    $term = array_pop($terms);
    

    Does that fix the issue?

    • udapud

    • July 12, 2013 at 7:15 pm

    @wells5609 @elliot
    Ok, so I managed to get this code to work to get a specific taxonomy ID (so I know the system is working)

    
    <p><?php // the_field('contact_info', 'product_brand_14'); ?></p>
    

    But trying to get the same terms of the current Term ID being displayed, doesn't work however. (In my “archive-brands” template.) Even with array_pop.

    
    <?php
     
    global $post;
     
    // load all 'category' terms for the post
     $terms = get_the_terms($post->ID, "product_brand");
     
    // we will use the first term to load ACF data from
    if( !empty($terms) )
    {
    	$term = array_pop($terms);
     
    	$custom_field = get_field('contact_info', 'category_' . $term->term_id );
     
    	// do something with $custom_field
    }
     
    ?>
    

    Thanks guys

  • Is ‘contact_info’ a field for Category terms or Brand terms? Based on the field that works, I’m guessing the latter, in which case you’ll need to change the 2nd get_field argument:

     $custom_field = get_field('contact_info', 'product_brand_' . $term->term_id);
    
    • udapud

    • July 13, 2013 at 7:43 am

    I’m afraid changing “category_” to “product_brand_” had no effect. It’s almost like that line has no way of getting the id of the current term. The address reads like this http://site/?product_brand=1st-call-mobility where “1st-call-mobility” is the term of the product_brand taxonomy.

    “contact_info” is my ACF field slug, and “Brands” is a Custom taxonomy, whose slug is “product_brand”. Its a Woocommerce plugin, essentially for adding manufacturers to Woocommerce products.

    I’ve tried printing this code before and it just wasn’t picking up the terms custom field in the print either.

    Do I perhaps need to define “$custom_field” somewhere?

    • udapud

    • July 13, 2013 at 7:50 am

    No matter I fixed it. The problem was with “get_field” I replaced it with “the_field”

    
    <?php
     
    global $post;
     
    // load all 'category' terms for the post
     $terms = get_the_terms($post->ID, "product_brand");
     
    // we will use the first term to load ACF data from
    if( !empty($terms) )
    {
    	$term = array_pop($terms);
     
    	$custom_field = the_field('contact_info', 'product_brand_' . $term->term_id);
     
    	// do something with $custom_field
    }
     
    ?>
    
    • udapud

    • July 13, 2013 at 7:51 am

    By the way chaps thanks incredibly for the help, I was ready to gauge out my eyes. 🙂

    Mike J

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.