StoutLogic / acf-builder

An Advanced Custom Field Configuration Builder
GNU General Public License v2.0
794 stars 62 forks source link

Repeater Field values Empty on Frontend (there on backend) After DB Push to Staging #186

Open matgargano opened 2 months ago

matgargano commented 2 months ago

Environment:

Description: After creating repeater fields locally and pushing the entire database to a staging environment, the fields appear empty on the frontend. However, these fields are visible and populated in the backend. Manually rearranging a repeater field and saving causes the values to display correctly on the frontend. Code example below.

Steps to Reproduce:

  1. Create repeater fields locally using StoutLogic's acf-builder.
  2. Push the local database to a staging environment.
  3. Check the frontend in the staging environment; repeater fields appear empty.
  4. Make a change to the repeater fields in the backend (e.g., rearrange items) and save.
  5. Frontend now displays the repeater fields correctly.

Expected Behavior: Repeater fields should retain and display their values on the frontend after the database is pushed to staging without requiring any modifications.

Actual Behavior: Repeater fields are initially empty on the frontend after pushing the database to staging and require a backend modification to display correctly.

Additional Information:

<?php

namespace Kitchen\Blocks;

use StoutLogic\AcfBuilder\FieldsBuilder;

class Kitchen_Image  {

        const BLOCK_SLUG = 'kitchen-image';
    const BLOCK_NAME = 'Kitchen Image';

    public function init(){
        $this->add_fields();
    }

    public function add_fields(){

        add_action('acf/init', function() use($fields){
            if(function_exists('acf_add_local_field_group')) {
                acf_add_local_field_group($fields->build());
            }

        });
    }

    public function register_fields() {
        $section = new FieldsBuilder('section-' . sanitize_title(__FILE__)); // get filename, slugify and lowercase and append it to section
        $section
            ->addRepeater('images', ['max' => 5])
            ->addImage('image', ['return_format' => 'id'])
            ->endRepeater()
            ->setLocation('block', '==', 'acf/'  . self::get_name());
        return $section;
    }

    public static function get_name(){
        return sprintf('sw-%s', static::BLOCK_SLUG);
    }

}

in functions.php

$kitchen_image = new \Kitchen\Blocks\Kitchen_Image();
$kitchen_image->init();
Tanguy-Magnaudet commented 2 months ago

Hello,

I faced that problem long time ago because I was not using WP CLI when migrating the database :

wp search-replace http://localhost:3322 http://www.exemple.com --all-tables

You should also run those command after :

# Fix the action scheduler that may fail after the migration
wp action-scheduler fix-schema
# Flush the cache, just in case
wp cache flush

It most likely will solve yours too as this library doesn't interact with the data it just declare fields in PHP in a more convenient way.