Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #653743

    Hi there,

    While editing a post in the backend using the Advanced Layout Builder, I need to get the current post ID.
    I tried several suggestions like $post->ID, etc.
    But all suggested solutions work inside the loop.
    And while editing the post I am outside the loop.

    How can I get de ID of the current edited post?
    I need it to retrieve extra data from the database.

    Thanks, Jan

    #655079

    Hey JB_Walton,

    Thank you for using Enfold.

    I’m sorry but I’m not sure what you’re trying to do here. Are you creating another function? Have you tried the following?

    $post_id = $_GET['post'];
    

    OR

    global $post;
    $post_id = $post->ID;
    

    You can also try the get_the_ID or the theme’s avia_get_the_ID function.

    Best regards
    Ismael

    • This reply was modified 8 years, 5 months ago by Ismael.
    #655188

    Thanks Ismael,

    What I try to accomplish:
    I am developing another function / element for the Advanced Layout Builder:
    Get more info of the post I am editing. I want to create a drop-down selection field for the element, that show me the available custom fields, that I than can show on the frontend page of that post.

    I already started a (closed) thread for the particular custom fields items.
    But I have to find out myself or hiring a freelancer for this.
    I started developing the function myself.
    But I get stuck on the first thing to do: I need to get the current post-ID in the backend (editing the post).
    I tried all tips in the forum (including the things you mention), but they seem to be working only on frontend (the-loop).
    In backend these thing give me NULL of boolean(false) as result.

    So my primary question is: How do I get the post-ID of the currently edited post in backend?

    Best regards,
    Jan

    #656357

    Hi,

    Please try this snippet then edit one of the posts with the custom fields:

    add_action('admin_head', 'ava_admin_head', 10, 1);
    function ava_admin_head() {
    	global $post;
    	$id = $post->ID;
    	$customfields = get_post_custom($id); // get custom fields of the post
    	var_dump($customfields);
    }

    Let me know if it does anything. The code should display all custom fields of the edited post.

    Best regards,
    Ismael

    #656494

    Hi Ismael,

    Thanks.
    I inserted the snippet into the functions.php of my child theme.
    Than, when I open a post in the backend it shows me the array of all custom fields.
    Also, var_dump($id) shows me the post-id of the pos, currently edited.

    So far so good.

    Now I take the same code (from “global” to “var_dump”) and put it in popup_elements of the shortcode file blog.php.
    When opening the post and inserting Blog_posts element and clicking it, all i see is “bool (false)”.

    So inside the Avia Layout Builder, it wo’t show the custom fields (or $id).
    Or is there something I am missing here?

    Regards,
    Jan

    #658181

    Hi,

    there’s a plugin that shows you all the meta fields associated with the current Post being edited:
    https://wordpress.org/plugins/post-meta-inspector/

    Best regards,
    Josue

    #658345

    Thanks Josue,

    This helps me indeed to see all the post custom fields inside the editing screen in the backend.
    However, this does not solve my real problem.

    In the Avia Layout Builder I try to get all these custom fields as well.
    I created a new custom element for the Avia Builder: “Custom Field”.
    In there I want to create som post element to pop up when clicking the element (after inserting it).
    I am not able to get all of the available custom fields of the current post.
    First (simple) thing to do is “var_dump” the post ID.
    Whatever I try by getting the $post->ID it always returns NULL.

    I hope the problem is clear: I cannot get current post information inside an ALB element.

    Regards,
    Jan

    #658624

    Hi,

    You’ll have to use Ajax because ALB elements pop-up are based on it, there’s a JS Object (avia_globals) that gets created when a Post has ALB enabled, there you have access to post_id (avia_globals.post_id).

    Best regards,
    Josue

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.