pods icon indicating copy to clipboard operation
pods copied to clipboard

Getting metadata >> Pods logic vs WP logic

Open JoryHogeveen opened this issue 8 years ago • 13 comments

Related issues:

  • #1484
  • #1578
  • #5055
  • #5147
  • https://wordpress.org/support/topic/array-from-ae-pro-template-pods-relationships-fields/

Summary

This is a follow up of my conversation with @jimtrue at Slack (29-06-2016). I'll take post relationships as an example at first:

If I would setup Pods to have relationship from one post_type to another and use get_post_meta to get the relationship data, Pods filters this and returns the WP_Post property structure as an associative array.

Example without Pods logic:

$var = get_post_meta( $post_id, 'relationship_meta_key' );
print_r($var);

// returns:
// Note that the array key doesn't reflect the post id
Array
(
    [0] => // the post id
    [1] => // the post id
    etc.
)

Example with Pods logic:

$var = get_post_meta( $post_id, 'relationship_meta_key' );
print_r($var);

// returns:
// Note that the array key doesn't reflect the post id
Array
(
    [0] => Array
        (
            [ID] => // the post id
            [post_title] => // the post title
            [post_content] => // the post content
            [post_excerpt] => // the post excerpt
            [post_status] => // the post status
            etc.
        )
    [1] => Array
        (
            [ID] => // the post id
            [post_title] => // the post title
            [post_content] => // the post content
            [post_excerpt] => // the post excerpt
            [post_status] => // the post status
            etc.
        )
    etc.
)

Another example is with the recent oEmbed field. As a theme developer I'd like to have Pods power for giving a client a field to give their imput for an oEmbed field but I'd like to handle this myself in a theme. Currently the only way to do this is to use Pods functions to get the raw data or to use get_post_custom to get literally all post metadata (this function doesn't get filtered by Pods).

Example without Pods logic:

$var = get_post_meta( $post_id, 'oEmbed_meta_key' );
print_r($var);

// returns:
Array
(
    [0] => // RAW VAR FROM DB
)
// This is usable for whatever the developer would like to do

Example with Pods logic:

$var = get_post_meta( $post_id, 'oEmbed_meta_key' );
print_r($var);

// returns:
Array
(
    [0] => '<iframe scrolling="no" frameborder="no" src="VAR"></iframe>'
)
// This is restricted to the WP (and Pods) logic for oEmbed. 
// If I would like to use WP's oEmbed function but still would like to have 
// some other attributes or classes, this would be fairly difficult to do.

For me as a developer for plugins and themes the Pods logic for get_post_meta seems a bit odd. get_post_meta is a WP core function to get post meta directly from the database (or meta cache) so I don't expect to get "filtered-for-display" post data in this case. If I would like to display output with Pods logic I'd use pods_field_display( POD_NAME, ID, META_KEY ) or any other Pods method.

I'm not implying to to have a solution (since it's been in Pods for a while) but just would like to get this up for discussion.

The above is a result of Pods filtering the get_post_meta function with the display function of the Pods field. IMO get_post_meta is a function to get raw metadata from the database for use as you like but not to get metadata that has been filtered with various functions.

This is merely my thought about this matter so any feedback would be very much appreciated! (of course all the above goes for other object metadata aswell (tax, user, etc.) )

JoryHogeveen avatar Jun 30 '16 00:06 JoryHogeveen

@sc0ttkclark When do you thing this could be discussed? My recent comment on the address field is also related.

JoryHogeveen avatar Jan 10 '17 13:01 JoryHogeveen

Also related to the new WP Gallery output in Pods 2.6.8. get_post_meta() would return the gallery HTML instead of the attachment post ID's. I would still expect attachment post ID's in this case since I asked for metadata, not formatted HTML.

JoryHogeveen avatar Jan 18 '17 21:01 JoryHogeveen

Update on this issue. There are some filters/functions to disable Pods logic in your themes.

Disable everything: add_filter( 'pods_meta_handler_get', '__return_false' );

Disable object_type: pods_no_conflict_on('post') // Disable Pods meta handling for post types Possible values: post / taxonomy / media / user / comment / settings

JoryHogeveen avatar Mar 10 '17 00:03 JoryHogeveen

When is this supposed to run?

add_filter( 'pods_meta_handler_get', '__return_false' );

I've just spend 3 hour trying to get to the bottom of this and it seems that pods->run functions which gets run on __construct triggers everything before the filters can be set via functions.php.

raptor235 avatar Apr 24 '17 15:04 raptor235

@JoryHogeveen would appreciate a quick response... this is a blocker for looming deadline

thank you

raptor235 avatar Apr 24 '17 15:04 raptor235

@JoryHogeveen I'm absolutely not trying to be demanding here by any means, just would love to hear from you if you're around.

raptor235 avatar Apr 24 '17 15:04 raptor235

Also tried the pods_no_conflict_on but it seems to be ignored... I see that polylang query is added to the join statement by pods.

pods_no_conflict_on( 'taxonomy' );
$catalogs         = get_term_meta(2520,'catalogs');
var_dump($catalogs);

raptor235 avatar Apr 24 '17 16:04 raptor235

Hi @raptor235 I honestly never actually used it. I just posted it here since @sc0ttkclark noted me for these filters.

Maybe add_filter( 'pods_meta_handler_get', '__return_false', 99 ); ?

Let's continue on Slack on your actual problem to keep this issue on topic :)

JoryHogeveen avatar Apr 24 '17 17:04 JoryHogeveen

as the question surfaced in the Beaver Community too - because of the way how we return image relations vs the expected id - any new ideas?

quasel avatar May 13 '17 23:05 quasel

We ran into the exact same issue with admin columns when writing the 'editing' part of each Pods field. The get_post_meta becomes unreliable with Pods as it tries to turn it into a display method. We need to reliably get the database value with get_post_meta, without Pods interfering with it.

Like @JoryHogeveen suggested, introducing a display function get_pods_field() for a Pod field would solve this. You could then remove the hook into get_post_meta.

tobiasschutter avatar Nov 15 '17 10:11 tobiasschutter

@sc0ttkclark Why is this closed?

JoryHogeveen avatar May 04 '18 07:05 JoryHogeveen

It's not a perfect system, but that's why it works -- because people will ping the closed issues that don't need to be iceboxed :)

sc0ttkclark avatar May 04 '18 22:05 sc0ttkclark

I personally agree that the behavior of get_post_meta and get_term_meta should not be changed by Pods. That being said, I'm sure this is a breaking change. Would it be possible for Pods to introduce some functions like pods_get_post_meta_raw and pods_get_term_meta_raw? That way, we can get around the filter on a case to case basis.

AnthonyIacono avatar Apr 27 '20 19:04 AnthonyIacono

Closing this issue. The latest versions of Pods now includes settings which can disable this behavior if you want this.

JoryHogeveen avatar Jul 13 '23 19:07 JoryHogeveen