Support » Plugin: SmartCrawl WordPress SEO checker, SEO analyzer, SEO optimizer » Custom Relationship Field Macro
Custom Relationship Field Macro
-
Hey,
Is there a way to get a relationship custom field meta value in a macro? I tried to use %%cf_META_KEY%% but it only worked with custom fields that aren’t relationship fields.
-
Hi @junkes
I hope you are doing well.
We will need to verify this, but to make sure we try to replicate the same on our end, are you creating the custom fields using any plugin like Advanced Custom fields?
Best Regards
Patrick FreitasHey, @wpmudevsupport12. Yes, I’m using Pods – Custom Content Types and Fields.
Hello @junkes !
I hope you’re having a great week!
This is because Pods perform a replacement of the metadata stored in the database on the fly. So for example even if the postmeta table contains a relationship field with value 123, what you will actually get when you try getting it with get_post_meta is an array with the post data filled in, not just 123.
Because of this SmartCrawl will not display anything as it checks if the data it got is an array or object and bails out.
To workaround that, you can add the following mu-plugin to wp-content/mu-plugins/pods-smartcrawl-relationship-macro.php to create custom macros:
<?php add_action( 'wds-known_macros', function( $replacements ){ global $post; $pods_field_slug = "test"; // please adjust based on your relationship field's actual slug $value = get_post_meta($post->ID, $pods_field_slug, true); $replacements['%%pods_relationship_id%%'] = $value["ID"]; $replacements['%%pods_relationship_title%%'] = $value["post_title"]; return $replacements; } );
Please make sure to adjust the $pods_field_slug variable to match the slug/name of your relationship field.
Here are other fields you can use provided by Pods with example data from my site:[ID] => 31 [post_author] => 1 [post_date] => 2021-09-22 20:54:39 [post_date_gmt] => 2021-09-22 20:54:39 [post_content] => [forminator_form id="30"] [post_title] => BAL test [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => bal-test [to_ping] => [pinged] => [post_modified] => 2021-09-22 20:54:39 [post_modified_gmt] => 2021-09-22 20:54:39 [post_content_filtered] => [post_parent] => 0 [guid] => https://dev0.local/?p=31 [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 0 [pod_item_id] => 31
Hope that helps!
Warm regards,
PawelWow, that was an amazing help, thank you, it worked perfectly!
If I would like to add more custom macros, for other fields, should I just add more tags like $pods_field_slug / $value / %%pods_relationship_id%% (obviously, changing the names of it for each new macro) or is there a better way to it?
Hi @junkes
Yes, you’d need to do it this way due to what my colleague already mentioned, that is – PODS doing some internal “replacements” on the fly.
As of now, this seems to be the best way.
Kind regards,
AdamAlso, if you want to disable the custom
get_post_meta()
handling for Pods, you can use this:define( 'PODS_ALLOW_FULL_META', false );
This may interfere with expectations of other integrations like certain Page Builders with how it anticipates
get_post_meta()
for fields it knows to be covered by Pods.That was awesome. You guys helped me so much. Thank you!
- The topic ‘Custom Relationship Field Macro’ is closed to new replies.