Get Post ID early in the posting process
-
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; }
- The topic ‘Get Post ID early in the posting process’ is closed to new replies.