Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get_field() return old values

I have a problem with get_field(). This function doesn't return the last inserted value with previously created fields. If i create a new field, get_field() doesn't return any value. All the value are correctly stored on database. What could be the problem? I have ACF 5.8.0 and Wordpress 5.2.

EDIT

<?php /* Template Name: Chi Siamo */ ?>
<?php get_header(); ?>

<?php $address_map = get_field('indirizzo_mappa'); ?>

<div id="aboutUs" class="container">

    <?php while ( have_posts() ) : the_post(); ?>
    <div class="title">
        <?php the_title(); ?>
    </div>
    <div class="content">
        <?php the_content(); ?>
    </div>
    <?php endwhile; ?>

</div>

<?php if ($address_map) : ?>
<h1 class="mapTitle"><?php echo __('WHERE WE ARE','noisegallery'); ?></h1>
<div id="map" data-address="<?php echo $address_map; ?>"></div>
<?php endif;?>

<div class="firma">Designed from scratch by <b><a target="_blank" href="http://hellodude.it">DUDE</a></b></div>

<?php get_footer(); ?>
like image 400
user2797134 Avatar asked Dec 09 '25 05:12

user2797134


1 Answers

One of the reasons (apart from cache not being cleared) is duplicated field_xxx post_name rows in wp_posts table. The reason is usually connected with exporting/importing fields or db changes during major plugin update (at least some users reported that).

When it happens, you get values from latest post in wp-admin and in acf/save_post hook, but when you're trying to get_field you get only values from oldest post of the same field_xxx.

You can easily check if that's the case with following query:

select post_name, count(ID) c, group_concat(ID) 
from wp_posts 
where post_type='acf-field'
group by post_name
having c>1;

If there are any values in results it's problably it. You can safely remove duplicated values starting from oldest post.

Of course you should create backup before doing that - export all acf fields to json file and make a dump od database.

In case there are many of them you can use this script which will leave highest ID of each duplicate fields and remove all duplicates.

include_once "wp-config.php";
include_once "wp-includes/wp-db.php";

$query = "select post_name, count(ID) c, group_concat(ID) gc from wp_posts where post_type='acf-field'
group by post_name
having c>1;";

$rows = $wpdb->get_results($query);

foreach ($rows as $row) {
    $duplicatedIds = explode(',', $row['gc']);

    $highestId = max($duplicatedIds);

    $keyToPreserve = array_search($highestId, $duplicatedIds);

    unset($duplicatedIds[$keyToPreserve]);

    $toRemoveIds = implode(',', $duplicatedIds);

    echo "- Removing duplicated posts from field {$row['post_name']}: $toRemoveIds. Preserving {$highestId}.\n";

    foreach ($duplicatedIds as $postId) {
        wp_delete_post($postId);
    }
}

wp_cache_flush();

After removing duplicated rows from database problem should not exist anymore. If you're using W3 Cache or any other cache plugin, you should purge it's cache as well.

like image 105
Konrad Gałęzowski Avatar answered Dec 10 '25 19:12

Konrad Gałęzowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!