Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When fetching an ACF field that returns WP_Post objects, convert them to Timber\Post #2981

Closed
75th opened this issue Apr 30, 2024 · 6 comments

Comments

@75th
Copy link

75th commented Apr 30, 2024

Problem

I'm on a project right now that fetches post reference ACF fields all over the place. These are returned as WP_Post objects (or lists thereof). This creates problems when the same template is sent the results of a Timber query in one place and ACF references in another; for instance, if a template gets a WP_Post, it has to use post.permalink, but if it gets a Timber\Post, it has to use post.link.

Solution

Timber\Post::meta() should convert any WP_Post objects it fetches into Timber\Post objects.

Alternatives

Timber\Post could implement the same interface as WP_Post alongside its own stuff, so developers don't have to care as much which kind of object they get. But I'm pretty sure some of those things have been deprecated and removed in the past. (Could be wrong.)

@gchtr
Copy link
Member

gchtr commented May 1, 2024

I think transform_value might be what you’re looking for here: https://timber.github.io/docs/v2/integrations/advanced-custom-fields/#transform-values-to-timber/php-objects.

@75th
Copy link
Author

75th commented May 1, 2024

D'oh, you're right, that's perfect. Sorry to bother!

@75th 75th closed this as completed May 1, 2024
@75th
Copy link
Author

75th commented May 1, 2024

This doesn't seem to handle it well when an ACF field that would be converted is empty:

TypeError: Timber\Factory\TermFactory::build(): Argument #1 ($term) must be of type WP_Term, WP_Error given, called in /var/www/html/vendor/timber/timber/src/Factory/TermFactory.php on line 59

The WP_Error in question is simply Empty Term. Is there a good way around this?

@75th 75th reopened this May 1, 2024
@75th
Copy link
Author

75th commented May 1, 2024

In TermFactory.php, the from_id method has this:

$wp_term = \get_term($id);

if (!$wp_term) {
    return null;
}

return $this->build($wp_term);

Looks like maybe that !$wp_term should be !$wp_term || $wp_term instanceof \WP_Error perhaps?

EDIT Or perhaps we should check empty($id) before calling get_term()

@Levdbas
Copy link
Member

Levdbas commented May 7, 2024

Hi @75th ,

Are you willing to create a PR for handling that specific behavior?

@Levdbas
Copy link
Member

Levdbas commented May 10, 2024

In TermFactory.php, the from_id method has this:

$wp_term = \get_term($id);

if (!$wp_term) {
    return null;
}

return $this->build($wp_term);

Looks like maybe that !$wp_term should be !$wp_term || $wp_term instanceof \WP_Error perhaps?

EDIT Or perhaps we should check empty($id) before calling get_term()

What I can see is that get_term only throws an error if the taxonomy does not exist. Was that your case as well @75th ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants