Title: custom shortcode extract data from post selection
Last modified: August 10, 2025

---

# custom shortcode extract data from post selection

 *  [plord12](https://wordpress.org/support/users/plord12/)
 * (@plord12)
 * [10 months ago](https://wordpress.org/support/topic/custom-shortcode-extract-data-from-post-selection/)
 * I see you describe creating a custom shortcode here [https://kb.mailpoet.com/article/160-create-a-custom-shortcode](https://kb.mailpoet.com/article/160-create-a-custom-shortcode),
   but how can I extract some data from a contained post ?
 * For example, I would like to get the start date & time from an event to add to
   the email body.
 * Thanks

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

 *  Plugin Support [Ojoma a11n](https://wordpress.org/support/users/geraltrivia/)
 * (@geraltrivia)
 * [9 months, 4 weeks ago](https://wordpress.org/support/topic/custom-shortcode-extract-data-from-post-selection/#post-18599964)
 * Hello there [@plord12](https://wordpress.org/support/users/plord12/) ,
 * Thank you for your question and for checking our guide on creating a custom shortcode.
   I understand you’d like to go a step further by extracting specific data from
   a post contained in your email.
 * While our guide shows how to create a custom shortcode, extracting post-specific
   details like event dates usually requires working with the post’s metadata. The
   exact approach will depend on how the event details are stored in your site. 
   For example, if you are using an events plugin, the start date and time might
   be saved as custom fields (post meta) or provided through that plugin’s own functions.
 * To achieve this, you’d likely need to extend the shortcode code from our article
   so it can:
    1. Identify the post or event you want to pull the data from.
    2. Retrieve the start date and time from the correct post meta or plugin function.
    3. Output that information in your email where the shortcode is placed.
 * Since this involves working with PHP and potentially a specific events plugin’s
   API, it may require the help of a developer to write or adjust the code for your
   setup. Our guide can serve as the base, but the event date and time retrieval
   logic would need to be added based on how your events are stored.
 * I hope this is helpful.
 *  Thread Starter [plord12](https://wordpress.org/support/users/plord12/)
 * (@plord12)
 * [9 months, 4 weeks ago](https://wordpress.org/support/topic/custom-shortcode-extract-data-from-post-selection/#post-18600010)
 * Many thanks for your response.
 * The first issue is how can I use the mailpoet API to identify the (first) post
   contained in the email. I was expecting to use something like :
 *     ```wp-block-code
       function mailpoet_custom_shortcode($shortcode, $newsletter, $subscriber, $queue, $newsletter_body, $arguments) {  if ($shortcode !== '[custom:date]') return $shortcode;     // whats the API to return the first post contained in the newsletter ?  $firstpost = $newsletter->getPosts()[0];  // once we've got access to the first post, probabally need to check  // its an event and then find the event's metadata  $startdate = $firstpost->getStartDate();    return $startdate;}
       ```
   
 * Or perhaps I need to get the first post’s id and find it in the database ?
   Thanks.
    -  This reply was modified 9 months, 4 weeks ago by [plord12](https://wordpress.org/support/users/plord12/).
 *  Plugin Support [Ojoma a11n](https://wordpress.org/support/users/geraltrivia/)
 * (@geraltrivia)
 * [9 months, 3 weeks ago](https://wordpress.org/support/topic/custom-shortcode-extract-data-from-post-selection/#post-18604459)
 * Hello again [@plord12](https://wordpress.org/support/users/plord12/) ,
 * Thank you so much for your thoughtful follow-up and for sharing the code example
   you’re working on. I completely understand why you’d like to pull the first post
   from a newsletter and use its metadata in your shortcode.
 * MailPoet’s API does not currently provide a direct method like `$newsletter->
   getPosts()` to access post objects within a newsletter. The posts are stored 
   as content inside the newsletter body rather than as objects that can be programmatically
   queried in that way. Because of this, there isn’t a built-in API call you can
   use to directly extract the first post and its metadata when generating a shortcode.
 * The approach you mentioned, retrieving the post ID from the database and then
   using WordPress functions like `get_post_meta()` to fetch the event metadata,
   would likely be the right path forward. Since this involves custom development
   and integration with the specific events plugin you’re using, it goes beyond 
   what we’re able to support directly under our support policy.
 * If you’re comfortable with PHP, you could extend the shortcode by parsing the
   newsletter content to find the first post’s ID and then use WordPress functions
   to fetch the event start date or other custom fields. Alternatively, a developer
   familiar with MailPoet and your events plugin should be able to help you craft
   a solution tailored to your setup.
 * If you need help with custom code, we recommend reaching out to someone from 
   [Codeable](https://www.codeable.io/partners/mailpoet/?ref=xxVTOd) (affiliate 
   link).
 * I’m really sorry we can’t provide a ready-made code fix here, but I hope this
   clarifies the limitations and points you in the right direction for building 
   out the functionality.

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

The topic ‘custom shortcode extract data from post selection’ is closed to new replies.

 * ![](https://ps.w.org/mailpoet/assets/icon-256x256.png?rev=3284564)
 * [MailPoet - Newsletters, Email Marketing, and Automation](https://wordpress.org/plugins/mailpoet/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/mailpoet/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/mailpoet/)
 * [Active Topics](https://wordpress.org/support/plugin/mailpoet/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/mailpoet/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/mailpoet/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [Ojoma a11n](https://wordpress.org/support/users/geraltrivia/)
 * Last activity: [9 months, 3 weeks ago](https://wordpress.org/support/topic/custom-shortcode-extract-data-from-post-selection/#post-18604459)
 * Status: not resolved