corcel-acf
corcel-acf copied to clipboard
ACF fields on taxonomy
Hello!
I was wondering if there is a possibility to access acf custom fields associated to a taxonomy. From what I have seen and read, you are accessing the wp_postmeta table, while the taxonomy meta are stored in the wp_termmeta table, so maybe it is not feasible today to access custom fields of taxonomies.
But maybe I'm wrong, can it be?
For example, I was thinking of a use similar to this:
$magazine = Magazine::where('taxonomy', 'magazine')->slug('218')->first()->acf->custom_field;
Hi @lopandpe ,
sorry for the late feedback, I actually wanted to solve your issue with a proper CR, but I cannot find the time. I can explain a workaround though:
This plugin accesses a post's meta data via some helper methods (getMeta()), which uses the wp_postmeta table you mentionend. The necessary helper methods exist in Corcel\Model\Post, but not in Corcel\Model\Taxonomy, that is why the plugin only works with posts right now. But if you define (read "copy") the necessary methods into you taxonomy model, the integration works (I am actually using this right now on a live website, just never found the time to implement it properly into the plugin)
Here is the workaround I am using right now:
<?php
use Corcel\Model\Taxonomy as BaseTaxonomy;
use Tbruckmaier\Corcelacf\AcfTrait
class Taxonomy extends BaseTaxonomy
{
use AcfTrait;
/**
* copied from Corcel\Concerns\MetaFields
*/
public function getMeta($attribute)
{
if ($meta = $this->meta->{$attribute}) {
return $meta;
}
return null;
}
/**
* copied from Illuminate\Database\Eloquent\Model
*/
public function newInstance($attributes = [], $exists = false)
{
// This method just provides a convenient way for us to generate fresh model
// instances of this current model. It is particularly useful during the
// hydration of new objects via the Eloquent query builder instances.
$model = new static;
$model->exists = $exists;
$model->setConnection(
$this->getConnectionName()
);
$model->setTable($this->getTable());
$model->mergeCasts($this->casts);
$model->fill((array) $attributes);
return $model;
}
/**
* copied from Illuminate\Database\Eloquent\Model
*/
public function newFromBuilder($attributes = [], $connection = null)
{
$model = $this->newInstance([], true);
$model->setRawAttributes((array) $attributes, true);
$model->setConnection($connection ?: $this->getConnectionName());
$model->fireModelEvent('retrieved', false);
return $model;
}
}
Hope this helps!