• Hi there,

    since version 5.0.0 deprecated many filters in favor of the_seo_framework_meta_render_data, I struggle a bit with adjusting my code.

    Currently, I use the filter the_seo_framework_description_output, where I first check if I need to output the description of another post (something like an ancient post of the current). This was easily possible by just requesting the metadata post meta of the ancient post.

    However, at least for the new version it looks more like a hacky workaround. So the question is: how do I get the metadata of a different post inside the filter the_seo_framework_meta_render_data (I’m aware that I need to remove the filter inside my filter before getting the metadata of the different post in order to prevent recursion)?

    Best regards,
    Matthias

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Sybre Waaijer

    (@cybr)

    Hi Matthias,

    I have posted an example snippet of how to use the filter here: https://gist.github.com/sybrew/272b746296114cd93c3320e9544a1618.

    If you have the old code for me, I can transform it quickly into the new one for you. This will help me understand the limitations I may have unintentionally imposed.

    If you wish to get a description of another post (e.g., post ID 42), you can use tsf()->description()->get_description( [ 'id' => 42 ] );. You can replace get_description() with get_custom_description() and get_generated_description() — they all work the same. More methods can be found here.

    To get metadata directly, you can use tsf()->data()->plugin()->post()->get_meta_item( '_genesis_description', $post_id ). You can also replace get_meta_item() with get_meta( $post_id ) to get all post metadata at once. More methods can be found here.

    There shouldn’t be a concern for recursion since the content of the the_seo_framework_meta_render_data filter is static. It immediately writes to a referencable variable and back again, but that’s just an Easter egg to condense code, and only there because I already chose to make a reference.

    You can treat the filter like any other; it should only run once per request, as action wp_head runs only once. I’m unsure when or why two pages should be generated in a single request, but your code may explain this to me.

    Thread Starter Matthias Kittsteiner

    (@kittmedia)

    Thank you for your answer and your explanation!

    I’m currently on vacation and will return after Christmas and check it out. But so far, it looks promising. I will definitely update my answer here as soon as I’m implementing it.

    Quick background for the reason: we have some template pages and a logic where based on another CPT (locations), a sub page of a location inherits data from the template pages. And these inheritance includes these metadata (and in this case I just need to make sure that shortcodes are executed there).

    Plugin Author Sybre Waaijer

    (@cybr)

    Thank you for the details. I don’t think recursion should be a concern in your case.

    In TSF v5.0, I added smarter caching, so fetching data from other posts will be much quicker, and won’t overwrite the current post’s cache. Excuse the childish Easter eggs. ??

    Let me know if you’re stumbling upon anything or think something could be improved.

    Merry Christmas!

    Thread Starter Matthias Kittsteiner

    (@kittmedia)

    Interestingly, tsf()->description()->get_description( [ 'id' => 42 ] ); didn’t work for me. The PHP process completely died without an error message. However, tsf()->data()->plugin()->post()->get_meta_item( '_genesis_description', $post_id ) worked fine. Thanks! ??

    megamatias

    (@megamatias)

    I can’t get this to work on my blog page (home). It always returns the blog main page description, not the individual posts description.

    • This reply was modified 9 months ago by megamatias.
    • This reply was modified 9 months ago by megamatias.
    • This reply was modified 9 months ago by megamatias.
    Plugin Author Sybre Waaijer

    (@cybr)

    Hi @megamatias,

    Could you please open a new topic so that we can dive into this issue for your site specifically?
    https://tsf.fyi/support/tsf/new-topic

    It’s likely to do with get_the_id() (queried post) versus get_queried_object_id() (queried main object). You can use tsf()->query()->get_the_real_id() to accurately get the main loop’s ID, if any.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to get metadata of different post’ is closed to new replies.