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
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));
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.
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
}