Title: shortcode including the ID variable
Last modified: May 10, 2023

---

# shortcode including the ID variable

 *  Resolved [project13x](https://wordpress.org/support/users/project13x/)
 * (@project13x)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/shortcode-including-the-id-variable/)
 * Hi,
 * I am using this plugin together with a shortcode.
 * I created a shortcode to get a specific field. But I need to include the ID of
   the current post.
 * However it doesn’t seem to get the id.
 * See my shortcode exmaple below
 * function user_details() {
   add_shortcode(‘au_display_user’, ‘func_user_details’);}
   add_action(‘init’, ‘user_details’);function func_user_details($atts){extract(
   shortcode_atts(array(‘p_id’ => ”,), $atts));$user_names = get_post_meta($p_id,‘
   agent’, true);//print_r($user_names);$return_string = ”;$args = array(‘post_type’
   => ‘agent’, ‘post__in’ => $user_names, “posts_per_page” => 1);$q = get_posts(
   $args);foreach ($q as $p) :$a = $p->ID;$email = get_post_meta($a, ’email’, true);
   $return_string .= $email;endforeach;return $return_string;}
 * I am using the following shortcode [dynamictext userdetails “au_display_user 
   p_id=’ID'”]
 * It displays an email address but not from the correct post id.
 * Thanks in advance!
    -  This topic was modified 3 years, 1 month ago by [project13x](https://wordpress.org/support/users/project13x/).

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [project13x](https://wordpress.org/support/users/project13x/)
 * (@project13x)
 * [3 years ago](https://wordpress.org/support/topic/shortcode-including-the-id-variable/#post-16728638)
 * Nevermind. I can just put the ID request in the shortcode itself.
 * Solved
 *  Plugin Author [Tessa (they/them), AuRise Creative](https://wordpress.org/support/users/tessawatkinsllc/)
 * (@tessawatkinsllc)
 * [3 years ago](https://wordpress.org/support/topic/shortcode-including-the-id-variable/#post-16735014)
 * So I was able to review your code snippet.
 * In your parameters for the `get_posts()` function, you’re passing `$user_names`
   as a value for `post__in` which, unless it’s an array of post IDs, it won’t return
   what you’re intending because:
 * > **`post__in`** (_array_) – use post ids. Specify posts to retrieve. **ATTENTION**
   > If you use sticky posts, they will be included (prepended!) in the posts you
   > retrieve whether you want it or not. To suppress this behaviour use `ignore_sticky_posts`.
   >  [WordPress Documentation for WP_Query](http://post__in (array) – use post ids. Specify posts to retrieve. ATTENTION If you use sticky posts, they will be included (prepended!) in the posts you retrieve whether you want it or not. To suppress this behaviour use ignore_sticky_posts.)
 * It looks like the purpose of your function is to return the email address associated
   with the meta key `email` in the post. In which case, this is how I’d rewrite
   that function:
 *     ```wp-block-code
       function func_user_details($atts)
        {
   
           extract(shortcode_atts(array(
               'p_id' => '',
           ), $atts));
   
           // Sanitize post ID that was passed as a value
           $p_id = sanitize_text_field($p_id);
   
           // Validate value and cast as an integer
           if (is_numeric($p_id)) {
               $p_id = intval($p_id);
   
               // Retrieve the post by ID
               $post = get_post($p_id); // @see https://developer.wordpress.org/reference/functions/get_post/
               if (!is_null($post)) {
   
                   // Access the post's meta to get your email address, sanitize
                   $email = sanitize_text_field(get_post_meta($post->ID, 'email', true));
   
                   return esc_attr($email); // Return the email address
               }
           }
           return ''; // Return empty string otherwise
       }
       ```
   
 * I always recommend using the most appropriate [sanitizing](https://developer.wordpress.org/apis/security/sanitizing/)
   and [escaping](https://developer.wordpress.org/apis/security/escaping/) functions.
 * While reviewing your snippet though, I’m wondering if the email address you’re
   trying to pull is actually that of the users in your `agent` meta associated 
   with the field. In which case, this function doesn’t do that but I can write 
   you one that does.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘shortcode including the ID variable’ is closed to new replies.

 * ![](https://ps.w.org/contact-form-7-dynamic-text-extension/assets/icon-256x256.
   png?rev=3019574)
 * [Contact Form 7 - Dynamic Text Extension](https://wordpress.org/plugins/contact-form-7-dynamic-text-extension/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/contact-form-7-dynamic-text-extension/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/contact-form-7-dynamic-text-extension/)
 * [Active Topics](https://wordpress.org/support/plugin/contact-form-7-dynamic-text-extension/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/contact-form-7-dynamic-text-extension/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/contact-form-7-dynamic-text-extension/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [Tessa (they/them), AuRise Creative](https://wordpress.org/support/users/tessawatkinsllc/)
 * Last activity: [3 years ago](https://wordpress.org/support/topic/shortcode-including-the-id-variable/#post-16735014)
 * Status: resolved