Okay
  Public Ticket #2379158
get post id of link
Closed

Comments

  •  3
    Norman started the conversation

    I'm creating a widget with items (enable items). Each item has 1 links to  a post.  I'd like to print the post-ID of that post. How can you achieve that in twig?

    Can anybody give me some direction how to achieve this?

  •  1,175
    Amit replied

    are you using post list ?

  •  3
    Norman replied

    No I'm using a link. Can't use a post  list because I need some extra attributes on it. (like the hotspot x and y coordinates).

  •  3
    Norman replied

    (https://unlimited-elements.helpscoutdocs.com/article/81-php-integration-get-data-from-php)

    I just figured out from the documentation that you can call any PHP function with

    {{ do_action('some_action','param1','param2','param3') }}

    So now I'm  doing this..

    {{ do_action( 'url_to_postid', 'item.link' ) }}

    This wordpress function should return the postid from the slug. But no luck :( Any idea what's going wrong)?

  •  3
    Norman replied

    Ok.. what I learned and what works is this:

    I added a custom filter in my function.php

    add_filter( 'getID', 'get_my_id' );
    function get_my_id( $myslug ) { 
        $postid = url_to_postid( $myslug );
        return $postid;
    }
    

    In the html field (twig template) I'm doing this:

    {% set itemID = apply_filters('getID', item.link|raw ) %}
    {{ itemID }}
    

    This way I get the ID of the linked post. :)

  •   Max replied privately