Get post id in a function when edit/add a post
-
I am new to PHP and I am working in WordPress. I wrote a function in the functions.php file. My function is to update a custom field based on another field on the page when the user adds or edits a post. I am having trouble getting the post id in my function. How can I get the Post Id? What am I doing wrong?
Here is my function:
function get_postid() { global $post; $id = $post->ID; } add_action( 'admin_notices', 'get_postid' ); function set_post_sort_order() { /* Get post id */ $post_id = get_postid(); /* Does object exist */ if ( !$post_id ): /* Get which product taxonomy is selected. */ $terms = get_the_terms( $post_id, 'product' ); $sort_order = 2; if ( ! empty( $terms ) ) : foreach( $terms as $term ) { if ( $term->name == 'blah blah' ) : $sort_order = 1; endif; update_post_meta( $post_id, 'post_order', $sort_order ); } endif; endif; }
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Get post id in a function when edit/add a post’ is closed to new replies.