Support

Account

Home Forums Add-ons Options Page get_field('xxx', 'options') not returning value anymore

Solved

get_field('xxx', 'options') not returning value anymore

  • All of a sudden I can’t get return anything from get_field(‘value’, ‘options’).

    Here’s my code from a template file called inside the theme:

    <?php
    echo get_current_blog_id();
    switch_to_blog(1);
    echo get_current_blog_id();

    $images = get_field(‘background_images’, ‘options’);
    var_dump($images); // returns nothing

    restore_current_blog();
    //exit;
    ?>

    Not sure why this is happening, on the parent site the function is returning fine.

    On another script I had to use get_option instead of get_field to fix this issue.

    switch_to_blog($array[‘blog_id’]);

    if(!$array[‘header_heading’] = get_option(‘header_heading’)) {
    $array[‘header_heading’] = get_bloginfo(‘name’);
    }
    $array[‘type’] = get_option(‘options_site_type’);
    $array[‘entreprise’] = get_option(‘options_entreprise’);
    $array[‘director’] = get_option(‘options_director’);
    $array[‘state’][‘post_id’] = get_option(‘options_state’);
    $array[‘address’][‘phone’] = get_option(‘options_phone’);
    $array[‘address’][‘marker’] = get_option(‘options_marker’);

    restore_current_blog();

    Any reason why this would happen all of sudden? I’m using latest version of ACF/options.

  • Hi @alexlat20

    I would say the issue is most likely related to the blog switching.
    Your second snippet of code shows that you switch blogs to $array['blog_id'], but in your first snippet, you switch to blog 1.

    Could it be possible that the options page data is on another blog DB?

    Thanks
    E

  • The first snippet of code is ran on child themes, so needs to switch to the primary site to grab the related content. The second piece of code is a page for the primary site. Basically lists all the child sites and pulls and outputs relevant fields for each site.

    What’s weird about this is that it worked fine in the past, I decided to work on it again over the weekend… and it stopped working all of a sudden.. not sure if it’s related to a plugin upgrade or not?

  • Did another test with this code in header.php, which is shared by child/primary site:

    switch_to_blog(25);
    echo '<pre>';
    var_dump(get_field('phone', 'options')); 
    echo '</pre>';
    echo '<pre>';
    var_dump(get_option('options_phone'));
    echo '</pre>';
    restore_current_blog();
    exit;

    (CHILD SITE)
    string(12) “714 579 1642”
    string(12) “714 579 1642”

    (PRIMARY SITE)
    string(2) “91”
    string(12) “714 579 1642”

  • Hi @alexlat20

    Perhaps the issue is related to cache?

    ACF will be caching the value of ‘phone’ for post_id of ‘options’.

    When you run the second code, ACF is returning what it found previously because it does not take into account the blog_id

    I’ll add this to the to-do to fix caching for blog switching!

    Thanks
    E

  • I did a little testing and the values both return for the first child site I created:

    string(10) “xx”
    string(10) “xx”

    The difference between the first child site and the other ones is that the theme options are hard coded inside the child-theme using the export feature…. hmmm any ideas?

  • Hi @alexlat20

    I’m not sure I understand your last reply. I’m still pretty sure the issue is with ACF caching the value (from the first blog), then when you get the value from the second blog, the cached value is returned.

    This is something that I need to test and fix

    Thanks
    E

  • Is there a function I can use to clear the cache or should I pull from the options table for the time being?

  • Hi @alexlat20

    Please use the get_option function for now as this is a core issue which can’t be fixed by ‘clearing the cache’

    Thanks
    E

  • Hi @alexlat20

    Are you able to help debug this issue? By making some minor tweaks to a file, we will be able to test out the cache issue:

    Change 1: core/fields/_functions.php: line 126

    
    //update cache
    wp_cache_set( 'load_value/post_id=' . $post_id . '/name=' . $field['name'], $value, 'acf' );
    

    to

    
    //update cache
    wp_cache_set( 'load_value/post_id=' . $post_id . '/name=' . $field['name'], $value, 'acf_' . get_current_blog_id() );
    

    Change 2: core/fields/_functions.php: line 56

    
    $cache = wp_cache_get( 'load_value/post_id=' . $post_id . '/name=' . $field['name'], 'acf', false, $found );
    

    to

    
    $cache = wp_cache_get( 'load_value/post_id=' . $post_id . '/name=' . $field['name'], 'acf_' . get_current_blog_id(), false, $found );
    

    Let me know if making these 2 changes fixes the get_field problem.

    Also, you can test just removing the cache check completely by commenting out line 60.

    Thanks
    E

  • Hey,

    I changed the code and still the same results unfortunately.

  • Hi @alexlat20

    Thanks for the reply. Perhaps the issue is not with cache at all. This is good news for me, but unfortunately it does not provide a clear direction for your problem.

    Hoe are your debugging skills? Are you able to debug some of the ACF functiosn such as get_field (in the core/api.php function)?

    There must be a reason why ACF is either caching the value from the first get_field, or if ACF is not loading from the correct blog.

    I’ll attempt to setup a multisite locally and do some testing.

    Thanks
    E

  • Hi @alexlat20

    I had a thought. Do both blogs contain the same ACF fields?

    Thanks
    E

  • Thanks for your reply,

    Primary Site has it’s own set of fields.

    All child sites have unique fields from the primary site, I exported the fields to my functions.php so client wouldn’t need to re-create them when a new site is created.

    If it makes it easier, I can give you access to the dev site.

  • So I did a little debugging:

    api.php (get_field)

    if i do:

    if( is_array($field) )
    {
    var_dump($field); // returns
    var_dump($field[‘value’]); // null
    exit;

    $return = $field[‘value’];
    }

    http://pastie.org/private/grhoao9xwi76daw7rzkoq

    If I grab the last_query w/ wpdb

    $return = get_option(‘_’ . $post_id . ‘_’ . $field_name);
    global $wpdb;
    print_r($wpdb->last_query);

    SELECT post_id, meta_key, meta_value FROM wp_19_postmeta WHERE post_id IN (63)

    should be grabbing from wp_options from the primary blog no? shouldn’t be wp_19

  • Hi @alexlat20

    Can you please post the exact code you are using for this loop?

    Is the issue, when you switch blog to 19, WP looks for a database table with a prefix of WP_19_? In this case, should the prefix just be WP_?

    Perhaps there is some code you need to use in your loop to switch to the ‘default blog’ instead of blog 19?

    Thanks
    E

  • elliot, I fixed the issue.. after going through some functions in api, I found that you check to see if the key is registered or not.. well it wasn’t. Turns out I had my parent theme’s field functions defined in functions.php and only if the blog id was 1….

    problem solved, thanks for your help

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

The topic ‘get_field('xxx', 'options') not returning value anymore’ is closed to new replies.