Query - Dynamic Shortcodes

Explore the Query Dynamic Shortcode, a sophisticated tool for retrieving posts, users, terms, and Woo Orders.

Updated yesterday

The Query Dynamic Shortcode is a dynamic and advanced element in WordPress for efficiently retrieving a variety of data types. It's designed for users who need to extract specific information from posts, users, terms, or WooCommerce orders without delving into the complexities of WordPress functions.

Syntax

The basic syntax of the Query Dynamic Shortcode is:

{query:type}
  • type: Specifies the type of data to retrieve (posts, users, terms, woo-orders).

Key Arguments

Key arguments are specific to the type of query:

{query:type @key-arguments-here}

Examples

  1. Fetching a Specific Number of Posts

    {query:posts@posts_per_page=15}

    Retrieves 15 posts, overriding the default limit (10).

  2. Fetching Posts in a Category

    {query:posts@category_name=technology}

    Retrieves posts that are in the 'technology' category.

  3. Posts by Specific Author

    {query:posts@author=2}

    Fetches posts written by the author with ID 2.

  4. Exclude Certain Posts

    {query:posts@post__not_in="10,20"}

    Excludes posts with IDs 10 and 20 from the results.

  5. Retrieve Posts with Custom Fields

    {query:posts@meta_key=color&meta_value=blue}

    Searches for posts with a custom field 'color' set to 'blue'.

  6. Display Sticky Posts Only

    {query:posts@post__in=sticky}

    Shows only the posts that are marked as sticky.

  7. Retrieving Users by Role

    {query:users@role=editor}

    Fetches all users with the 'editor' role

  8. Listing Categories

    {query:terms@taxonomy=category}

    Retrieves all terms in the 'category' taxonomy.

  9. Extracting Completed Woo Orders

    {query:woo-orders@status=completed}

    Gathers all completed Woo orders.

  10. Extracting Recent Woo Orders

    {query:woo-orders@limit=5 status=processing}

    Gathers the 5 most recent processing Woo orders.

  11. Custom Post Type Retrieval

    {query:posts@post_type=students}

    Fetches all posts of the 'students' custom post type.

  12. Fetch Posts with Certain Tags

    {query:posts@tag="health,fitness"}

    Retrieves posts tagged with either 'health' or 'fitness'.

  13. Posts in Multiple Categories

    {query:posts@category_name="travel,food"}

    Fetches posts that are in both the 'travel' and 'food' categories.

  14. Retrieve Posts with a Specific Custom Field Value

    {query:posts@meta_key=event_date meta_value='2023-12-01'}

    Searches for posts with a custom field 'event_date' set to '2023-12-01'.

  15. Posts Sorted by Title

    {query:posts@orderby=title order=ASC}

    Lists posts in ascending order by their title.

  16. Users with Multiple Roles

    {query:users@role__in={array:subscriber contributor}}

    Fetches users who are either subscribers or contributors.

  17. Retrieve Users Ordered by Display Name

    {query:users@orderby=display_name order=ASC}

    Lists users in ascending order by their display name.

  18. Exclude Users with Certain Roles

    {query:users@role__not_in={array:administrator editor}}

    Excludes users with 'administrator' or 'editor' roles.

  19. Fetch Users with Specific Name

    {query:users@search='John Doe'}

    Retrieves users whose names include 'John Doe'.

  20. Include Specific User IDs

    {query:users@include={array:3 7 9}}

    Includes only users with IDs 3, 7, and 9.

Handling Results with the for Dynamic Shortcode

The for Dynamic Shortcode in WordPress is a powerful tool used in conjunction with other Dynamic Shortcodes like query:posts or query:users to iterate over and display the results they return. This section explains how to use the for shortcode to manage and present data retrieved by Query Dynamic Shortcodes.

Understanding the Output of Dynamic Shortcodes

  • {query:posts}: Returns an array of post IDs.

  • {query:users}: Provides an array of user IDs.

  • {query:terms}: Outputs an array of term IDs.

  • {query:woo-orders}: Returns an array of WooCommerce order IDs.

Using the for Dynamic Shortcode

The for Dynamic Shortcode iterates over arrays or iterable values and allows the integration of templates or placeholders in each iteration.

Syntax:

{for:placeholder iterable_value [template_or_value]}
  • placeholder: A name that serves as a placeholder for the current value in the loop. This can be retrieved later with the get Shortcode.

  • iterable_value: The array or iterable value to loop over.

  • template_or_value: A template or a single shortcode that will be displayed for each iteration.

Examples

  1. Basic Loop

    <ul> 
    {for:post-id
    {query:posts}
    [<li>{post:title @ID={get:post-id}}]
    }
    </ul>

    This example will iterate over post IDs, displaying each Post Title in a list item.

  2. Loop with Separator

    {for:user-id
    {query:users}
    [{user:nickname @ID={get:user-id}]
    @ sep=', '
    }

    This will iterate over user IDs and separate their nicknames with a comma.

These examples demonstrate how to effectively utilize the for Dynamic Shortcode to handle and display data returned by various Dynamic Shortcodes. This method provides a flexible way to customize the presentation of queried data in WordPress.

Counting Posts with the Query Dynamic Shortcode

The ability to count posts, users, terms, or WooCommerce orders is a crucial feature of the Query Dynamic Shortcode. This functionality is especially useful for obtaining a quick summary of the number of items in a specific category or that meet particular criteria.

How It Works

Counting is executed by appending the count filter to the basic syntax of the Query Dynamic Shortcode, and then specifying the key arguments. The general syntax for counting is:

{query:type | count @keyargs}

Where type can be posts, users, terms, or woo-orders, and keyargs are the specific arguments relevant to the type.

Keep in mind that retrieving a very large number of posts, terms, users or WooCommerce orders at once can have a significant impact on performance, especially on sites with a lot of data. In this case, it is recommended to put the result in a Dynamic Shortcodes cache.

Usage Examples

  1. Counting Users by Role

    To count the number of users with the 'editor' role

    {query:users | count @role=editor}

  2. Counting Posts in a Category

    To count the number of posts in the 'technology' category

    {query:posts | count @category_name=technology posts_per_page=-1}

  3. Counting WooCommerce Orders with a Certain Status

    Totally the number of WooCommerce orders with a 'completed' status:

    {query:woo-orders | count @status=completed limit=-1}

If you want to count posts in WordPress or orders in WooCommerce, you need to set posts_per_page to -1 for posts and limit to -1 for WooCommerce orders, respectively. This is because the default value for posts_per_page in posts is 5, while for WooCommerce orders, the default value of limit is 10. It's also important to note that for terms and users, there is no need to set such parameters as they already retrieve all elements by default.

Special Privileges

The Query Dynamic Shortcode should be used within a Power Shortcode.

Did this answer your question?