• For efficiency reasons I started to create my posts via a script that uses the WP API. In one of my plugins that processes post content data I need the (future) post ID as early as possible, even before any WP functions (like get_queried_object_id(), get_the_ID(), global $post – $post->ID;) would return a post ID. In order to get the post ID I started to mess around with add_action hooks. With wp_insert_post for example (https://wordpress.stackexchange.com/questions/45419/how-to-get-future-id-for-post-which-havent-been-created-yet) I can fetch the post ID very early but can only use it inside my custom function and nowhere else. What I tried so far was using public variables inside a class, trying to globalize the $post_id variable or pass it to another function… all to no avail.

    Function I tried:

    add_action( 'wp_insert_post', 'post_id_action_hook', null, 2 );
    		function post_id_action_hook($post_id, $post)
    		{
    			global $pub_id;
    			$pub_id = $post_id;
    		}
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m assuming you’ve declared global $pub_id; in every place where you want to use that value. You may already know that in PHP you must re-declare it global within every scope that you want to use it in.

    If you’ve done so and the value is not available, either you’ve tried to use the value too early, before “wp_insert_post” has fired, or you’re trying to use it in a separate request. Globals are not persistent between requests. You could temporarily save values between requests as transients, or make use of session vars.

    Thread Starter andreasvolt

    (@andreasvolt)

    Thank you for the answer.

    I have always re-declared the variable with global. Your second hint will probably be the right one. I assume that I use the variable too early, before “wp_insert_post” has fired.

    I’m trying to modify a plugin (Auto Upload Images) so that depending on the custom post type the image is imported into a different folder.

    I try to query the custom post type in line 206 with the post id, which unfortunately can no longer be determined when posting via the Rest Api.

    The post id determination with the help of the wordpress hook I try in the lines 198 – 203.

    How would i have to change the code to run the “wp_insert_post” hook before my value usage?

    PHP Code:
    https://pastebin.com/YvdhmZNg

    Moderator bcworkz

    (@bcworkz)

    The add_action() hook needs to be done before the initial API call to upload the image at line 190. I don’t think this will give you the ID you want though. I think you’d get the attachment ID. However, you should be able to get the parent post ID from the attachment’s parent property. The attachment parent is the post type you’re after.

    Ideally, you’d want to affect the upload destination before it is moved from /tmp/ for the first time. I’m not sure where you’d get the post type though. Attachment meta data is yet to be created when the move first occurs. It must be somewhere though, since the attachment meta data gets it assigned as its parent property.

    For conventional uploads, there are filters in _wp_handle_upload() you can use to alter the final destination. I’m not sure if the API still uses that function though.

    It’s not a great idea to directly modify plugin code. Updates will wipe out your changes and you’d need to re-apply them. If at all possible, do everything through available hooks from your own plugin. A lot of plugins aren’t very good at providing useful hooks, so you may not have a lot of choice in the matter. Even without plugin hooks, you can often accomplish things without modifying the plugin by using WP core hooks, since plugins frequently rely upon core code to accomplish things.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get Post ID early in the posting process’ is closed to new replies.