Post - Dynamic Shortcodes

Learn how to use the Post Dynamic Shortcode to retrieve post-related data in WordPress.

Updated over a week ago

The Post Dynamic Shortcode is a powerful tool designed to fetch and display specific data related to WordPress posts. It allows for fine-tuned data retrieval by specifying post fields, using key arguments for additional filtering, and even accessing post meta that isn't publicly exposed. Ideal for developers and content creators, it enhances the flexibility and control of content within WordPress pages or posts.

Syntax

The syntax for the Post Dynamic Shortcode is as follows:

{post:field@keyargs}

The field represents the specific post data you want to retrieve, such as the post title, author, or custom meta. The @keyargs allows you to specify additional filters and options, such as the post ID or whether to return all metadata associated with a post.

Key Arguments

  • id: Specifies the ID of the post from which to retrieve data. If not provided, the current post is used.

  • all: A boolean key argument that, when set, returns all metadata associated with the specified post field.

  • taxonomy: When retrieving terms, this argument specifies which taxonomy's terms to retrieve (default is category).

Examples

  1. Retrieve the Title of a Specific Post: {post:title@id=42} This shortcode will fetch and display the title of the post with the ID 42.

  2. Get the Excerpt of the Current Post: {post:excerpt} This example will return the excerpt for the current post being viewed.

  3. List Terms Associated with a Post: {post:terms@taxonomy=category id=10} Here, the shortcode lists terms from the 'category' taxonomy associated with the post that has the ID 10.

  4. Access a Custom Field Value: {post:custom_field_name@id=15} This retrieves the value of 'custom_field_name' from the post with the ID 15, assuming the user has the proper privileges.

  5. Retrieve All Meta Data for a Post: {post:_custom_meta_key@all id=23} This will return all values for the meta key '_custom_meta_key' from the post with ID 23, again, provided the user has adequate privileges.

Special Privileges

Accessing certain post meta, particularly those starting with an underscore (which are typically private), requires the shortcode to be used within a Power Shortcode that has elevated privileges. Additionally, if the post is not publicly visible (e.g., not published), elevated privileges are also required to retrieve its data. These privileges ensure sensitive information is protected and only accessible by authorized users.

Did this answer your question?