- How do I install corcel-acf in a Laravel project?
- First, install Corcel (`tightenco/corcel`) and `wp-data` via Composer. Then add `wp4laravel/corcel-acf` as a dependency. Configure your WordPress database connection in `config/corcel.php`, and run `php artisan corcel:install` to set up the bridge. ACF fields will then be accessible via Corcel models like `Post::with('acf')->find(1)`.
- Does corcel-acf support ACF Pro fields like repeaters or galleries?
- Yes, corcel-acf supports all ACF field types, including repeaters, galleries, and WYSIWYG editors. These are serialized into Eloquent relationships (e.g., `hasMany` for repeaters) or JSON structures, depending on your Corcel configuration. Check the [README](https://github.com/WP4Laravel/corcel-acf) for field-type-specific examples.
- Can I use corcel-acf with Laravel 9 or 10?
- Corcel ACF is compatible with Laravel 8+, including Laravel 9 and 10. However, always verify compatibility with the latest Corcel version, as minor Laravel updates may require adjustments. The package leverages Corcel’s Eloquent integration, which is actively maintained for modern Laravel.
- How do I query ACF fields for a WordPress post in Laravel?
- Use Corcel’s Eloquent syntax: `Post::find($id)->acf` returns the ACF data as an array or object. For nested fields (e.g., repeaters), use relationships: `Post::with('acf.repeater_field')->find($id)`. You can also chain methods like `whereHas('acf', function($query) { ... })` for filtering.
- Will corcel-acf work if my WordPress and Laravel apps are on separate servers?
- Yes, but your Laravel app must still access the WordPress database (or a replica) to query ACF data. For production, consider a read-only database replica to avoid load on WordPress. If real-time sync is critical, explore caching (Redis) or denormalizing ACF data into Laravel tables via a cron job or queue.
- How do I handle ACF field updates in WordPress that affect Laravel?
- Corcel ACF is read-heavy, so updates in WordPress won’t automatically reflect in Laravel. To sync changes, listen for WordPress `save_post` or ACF-specific hooks (e.g., `acf/save_field`) and trigger a Laravel job to refresh cached ACF data. Alternatively, use a scheduled task to repopulate ACF data periodically.
- Is there a performance impact when querying ACF data for many posts?
- Yes, N+1 queries can occur when fetching ACF data for multiple posts. Mitigate this by eager loading: `Post::with('acf')->get()`. For complex setups (e.g., deeply nested repeaters), consider caching ACF data at the model level or using Laravel’s query caching. GraphQL (e.g., Laravel GraphQL + Corcel) can also optimize payloads.
- Can I write ACF data from Laravel back to WordPress?
- Corcel ACF is primarily for reading ACF data. Writing requires custom logic to update `wp_postmeta` directly or via WordPress’s REST API. For hybrid workflows, consider using WordPress’s `wp_insert_post_data` filter or a dedicated service layer to handle writes while keeping reads efficient.
- What are the alternatives to corcel-acf for accessing WordPress ACF in Laravel?
- Alternatives include raw SQL queries against `wp_postmeta`, the WordPress REST API (with a Laravel client like Guzzle), or custom Laravel models that map ACF fields to database views. However, these lack Corcel’s Eloquent integration and ACF-specific optimizations. For headless setups, tools like WPGraphQL + Laravel GraphQL may also be worth evaluating.
- How do I test corcel-acf in a CI/CD pipeline?
- Test corcel-acf by seeding a WordPress-like database (e.g., using Laravel’s `DatabaseSeeder` with ACF test data). Use Docker to spin up a temporary WordPress environment for tests, or mock Corcel’s database layer with factories. Ensure your tests cover ACF field serialization, relationships, and edge cases like missing fields or nested data.