Support » Plugin: SmartCrawl WordPress SEO checker, SEO analyzer, SEO optimizer » Custom Relationship Field Macro

  • Resolved junkes

    (@junkes)


    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.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    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 Freitas

    Thread Starter junkes

    (@junkes)

    Hey, @wpmudevsupport12. Yes, I’m using Pods – Custom Content Types and Fields.

    Plugin Support Pawel – WPMU DEV Support

    (@wpmudev-support9)

    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,
    Pawel

    Thread Starter junkes

    (@junkes)

    Wow, 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?

    Plugin Support Adam – WPMU DEV Support

    (@wpmudev-support8)

    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,
    Adam

    Also, 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.

    Thread Starter junkes

    (@junkes)

    That was awesome. You guys helped me so much. Thank you!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom Relationship Field Macro’ is closed to new replies.