Skip Navigation

[Resolved] How can I get the id of a post being edited in a form?

This support ticket is created 2 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 5 replies, has 2 voices.

Last updated by Shane 2 years, 11 months ago.

Assisted by: Shane.

Author
Posts
#2265447

I need to get the id of the post being edited in a form for a PHP function I am building.
I have tried "get_the_ID()," but that gives me only the form id.
Is there a function I can call or...
truls

#2265703

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Truls,

Thank you for getting in touch.

Can you provide a bit more context on the issue for me. Where are you trying to get the post id. Is it after you submit the form or before ?

Also are you using the ID to display something specific on the form ?

Please let me know.
Thanks,
Shane

#2265829

Hi Shane.
I have a set of functions (see below) that check if a post has all the information registered that the post should have. Some of the information is categories, and some are relationships.
The functions return a warning if the information is not set.
This works great when viewing the post, but not when you view the post in the edit form.
The functions need the post id to check if the categories and relationships are set. I use the get_the_ID() function to get the post id, but when I view the post in the edit form, I get the form id.
I have tried to use the wpv-search-term shortcode in my shortcode to get the url parameter but got a fault.

Shortcode that works on post view:
[ais_check_if_category_is_set category="avtalekategori"]
Shortcode that does not works on edit form:
[ais_check_if_category_is_set category="avtalekategori" pid=[ wpv-search-term param="postid"]]

This function checks if a category is set.
[php]
function ais_check_if_category_is_set($atts)
{
extract(shortcode_atts(array(
'category' => ''
), $atts));

$id = get_the_ID();
if (is_object_in_term($id, $category)) :
$svar = '';
return $svar; else :
$svar = '<div class="ais-varselmelding"><span class="ais-varselmelding__content">' . ucfirst($category) . ' ikke valgt</span></div>';
return $svar;
endif;
}

#2265885

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Truls,

Given that you're doing this on the frontend then you should be able to use the [wpv-post-id] shortcode to get the post ID when added to the form.

Can you just add this shortcode to your form and then check to see if you're getting the correct ID. Then you can just pass it into your shortcode like this.

[ais_check_if_category_is_set category="avtalekategori" pid="[wpv-post-id]"]

Thanks,
Shane

#2266281

Hi Shane.
It still fails, but I found a workaround.
The wpv-post-id produces the form id and not the post id. And when I incorporate the shortcode in my shortcode, it generates a fault. The same happens when I try to use the ‘wpv-search-term’ shortcode. It seems that quotation marks inside a shortcode that are inside quotation marks that is inside a shortcode do not work. 😊
The shortcode is interpreted as a string, and parts of the shortcode are printed to the screen.

I ended up using an if statement like this in my function.php file:

if (empty(do_shortcode('[wpv-search-term param="postid"]'))) {
        $query_by_element = get_the_ID(); // ID of post to get relationship connections from
    } else {
        $query_by_element = do_shortcode('[wpv-search-term param="postid"]'); // ID of post to get relationship connections from
    }
#2266535

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Truls,

Happy to see that you're able to arrive at a solution that is acceptable.

If there are any concerns please let me know, if not then you can go ahead and mark this ticket as resolved.

Thanks,
Shane