Support

Account

Home Forums ACF PRO switch_to_blog() breaks get_field(), the_field(), get_post_meta() etc.

Solved

switch_to_blog() breaks get_field(), the_field(), get_post_meta() etc.

  • Hi @ all,

    I’m quite lost in an issue with ACF and a multisite switch_to_blog(). Even investigating for a couple of days didn’t bring a solution. So I hope to get some useful help here.

    I’m using switch_to_blog in the header to build menu items using the ‘wp_nav_menu_items’ filter. Here I’m going through a couple of pages and get the title and some fields from each site. That works quite fine and of cause I’m always switching back to get the last blog “restore_current_blog()”. Also after all this “ms_is_switched()” correctly outputs “null”. So I’m back on the page I want to be.

    The issue is now that asa I’m using get_field(), the_field(), get_post_meta() etc. the regular page on which I am returns the value of the field of another page.

    Example:

    – On blog 2 go through (switch_to_blog()) 3,4,5 and get the menu items. Currently switched back to blog 2 afterwards.
    – In the regular content of a CPT on blog 2 I receive get_title(), get_the_ID() and all other WO core functions correctly but get_field() returns the value from the post of blog 5 like it is stuck somewhere in the switch.

    I hope that someone has an idea how to solve this!

    Best regards,

    Philipp

  • I honestly don’t know what the problem could be and without setting up a site that does the same thing that yours does there’s no way I can debug it.

    Are you altering anything in the global $post? for example, are you running any loops that call the_post()? ACF uses the value in $post->ID to get values. If this is being alter, it could be the source of the problem.

    Another issue could be the post meta cache. If the post ID of the post on the other site is the same as the post ID of the post on the current site then WP itself could be getting the wrong values because it’s getting the values stored in the cache instead of getting the values from the database. If this is a possibility, you might want to clear the post meta cache before switching back to the current blog wp_cache_delete($post_id, 'post_meta');

  • Dear John,

    thank you very much for the fast and helpful answer. You’re right, the post ID’s were the same and somehow the cache was the root of the issue. I was now able to fix it by using wp_cache_delete($post_id, 'post_meta');.

    Cheers and best regards,

    Philipp

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

The topic ‘switch_to_blog() breaks get_field(), the_field(), get_post_meta() etc.’ is closed to new replies.