How to find a WordPress post by any other value than the post ID?

Locating a post via the post ID is fairly simple, but what if you want to find a post by, for example, the most recent date, or a specific post meta key and value pair?

In this article, we are going to show you how you can do that.

The correct endpoint: Get multiple posts

The correct webhook action needed for this use case is called “Get multiple posts” and is available within the WordPress integration, which is, by default, integrated into WP Webhooks.

How to get the most recent 10 posts from a custom post type

In this example, you are going to learn how to find and return the most recent posts (For the post type “post”).

Set up within a Flow automation

In case you are using our Flow feature, you can locate the flow of your choice, within your WordPress dashboard -> WP Webhooks -> Flows, and follow the steps down below:

  1. Open your Flow
  2. Add a new action and select the “WordPress” integration
  3. After the integration, please select the “Get multiple posts” action
  4. Once selected, you will see various arguments, of which the first one (called arguments) is the most important one as it will contain the specs used to find the posts. The WP Webhooks Pro flow screen with the get_posts webhook action
  5. Within that argument, please post the following JSON construct:
    {
      "post_type": "post",
      "order": "DESC",
      "orderby": "date",
      "posts_per_page" : 10
    }
  6. This JSON contains various arguments from the WP_Query class to determine which values to return. If you want to learn more about all available values, you can take a look at the Parameter section within the WP_Query documentation itself.
  7. After you pasted the JSON construct, continue the step and select “Fire webhook action” from the dropdown.
  8. Once you fired the action, you will see the 10 latest posts (in case the post type above exists at your end).

Once you followed the steps above, you successfully set up the get_posts webhook action.
In case you have difficulties editing the JSON: There are some great, free tools available online. We can recommend the JSON Editor Online.

Explanation of JSON values
Down below you will find an explanation for all of the JSON values.

  • post_type: This is the type of posts used to search for. Set it to “post” for the regular posts, or any other post slug for custom post types.
  • order: This defines the sort order of the found items. By default, we sort them as DESC, which refers to an order from the newest post to the oldest.
  • orderby: Refers to the type of value we want to filter the post for. In our case, we chose the date, which refers to the creation date of the post.
  • posts_per_page: Defines the number of posts available within the response of your request.

Setup with a direct webhook call

In case you are looking to create a direct connection with the “Get multiple posts” action (Which can only receive and return data from the “Get multiple posts” endpoint), please follow the steps down below:

  1. Head over in to your WordPress dashboard -> WP Webhooks -> Receive Data -> Get multiple posts
  2. Once there, you are able to create a webhook action URL by defining a name for it. You can name it e.g. find-posts
  3. After the URL is created, you are able to use that URL in combination with any service that supports webhooks and API calls. (In our example we are using Zapier).
  4. WIthin your preferred service, you can add the created action URL into the URL field.
  5. After, you can define the arguments within the payload or body of the request. If we are looking at the argument list inside of our plugin (On the same page as in step 1), we will see one argument called “arguments”, which is required and what we need. The Zapier POST action in combination with the get_posts action of WP Webhooks
  6. Within that argument, please post the following JSON construct:
    {
      "post_type": "post",
      "order": "DESC",
      "orderby": "date",
      "posts_per_page" : 10
    }
  7. This JSON contains various arguments from the WP_Query class to determine which values to return. If you want to learn more about all available values, you can take a look at the Parameter section within the WP_Query documentation itself.
  8. After you have added the JSON construct, you can test the step. Within the response, you will then see all the posts found for the specific action request.

Explanation of the JSON values
Down below you will find an explanation with all of the JSON values.

  • post_type: This is the type of posts used to search for. Set it t “post” for the regular posts, or any other post slug for custom post types.
  • order: This defines the sort order of the found items. By default, we sort them as DESC, which refers to an order from the newest post to the oldest.
  • orderby: Refers to the type of value we want to filter the post for. In our case, we chose the date, which refers to the creation date of the post.
  • posts_per_page: Defines the number of posts available within the response of your request.

Extra: Working with meta fields

Finding posts based on meta fields

It is also possible to find posts based on a given meta field key and value (This works as well with ACF fields).
To make it work, you need to define two more keys for the JSON within the “arguments” argument:

{
  "post_type": "post",
  "order": "DESC",
  "orderby": "date",
  "posts_per_page" : 10,
  "meta_key": "your_custom_meta_key",
  "meta_value": "your custom value"
}

Those two arguments can be used separately or together. Here is a short explanation about them.

  • meta_key: This field accepts the meta key of a post meta item within the post meta.
  • meta_value This refers to the value for a given post meta item within the post meta.

Adding meta fields to the response data

It is also possible to add meta fields, either custom meta fields, or ACF fields.
To make it work, please set the value (in bold) for the argument (in italic) within your argument definition.

  1. For custom fields: load_meta | yes
  2. For ACF fields: load_acf | yes

Defining one of those arguments will cause the meta values to be added to each of the found posts.

The response

Down below you will find an example response of the action request.

{
    "success": true,
    "msg": "Query was successfully executed.",
    "data": {
        "all": {
            "query": {
                "search": "Demo"
            },
            "query_vars": {
                "search": "Demo",
                "error": "",
                "m": "",
                "p": 0,
                "post_parent": "",
                "subpost": "",
                "subpost_id": "",
                "attachment": "",
                "attachment_id": 0,
                "name": "",
                "pagename": "",
                "page_id": 0,
                "second": "",
                "minute": "",
                "hour": "",
                "day": 0,
                "monthnum": 0,
                "year": 0,
                "w": 0,
                "category_name": "",
                "tag": "",
                "cat": "",
                "tag_id": "",
                "author": "",
                "author_name": "",
                "feed": "",
                "tb": "",
                "paged": 0,
                "meta_key": "",
                "meta_value": "",
                "preview": "",
                "s": "",
                "sentence": "",
                "title": "",
                "fields": "",
                "menu_order": "",
                "embed": "",
                "category__in": [],
                "category__not_in": [],
                "category__and": [],
                "post__in": [],
                "post__not_in": [],
                "post_name__in": [],
                "tag__in": [],
                "tag__not_in": [],
                "tag__and": [],
                "tag_slug__in": [],
                "tag_slug__and": [],
                "post_parent__in": [],
                "post_parent__not_in": [],
                "author__in": [],
                "author__not_in": [],
                "ignore_sticky_posts": false,
                "suppress_filters": false,
                "cache_results": true,
                "update_post_term_cache": true,
                "lazy_load_term_meta": true,
                "update_post_meta_cache": true,
                "post_type": "",
                "posts_per_page": 10,
                "nopaging": false,
                "comments_per_page": "50",
                "no_found_rows": false,
                "order": "DESC"
            },
            "tax_query": {
                "queries": [],
                "relation": "AND",
                "queried_terms": [],
                "primary_table": "wp_posts",
                "primary_id_column": "ID"
            },
            "meta_query": {
                "queries": [],
                "relation": null,
                "meta_table": null,
                "meta_id_column": null,
                "primary_table": null,
                "primary_id_column": null
            },
            "date_query": false,
            "request": "SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  WHERE 1=1  AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'cancelled' OR wp_posts.post_status = 'edd_subscription' OR wp_posts.post_status = 'expired' OR wp_posts.post_status = 'refunded' OR wp_posts.post_status = 'failed' OR wp_posts.post_status = 'revoked' OR wp_posts.post_status = 'abandoned' OR wp_posts.post_status = 'processing' OR wp_posts.post_status = 'active' OR wp_posts.post_status = 'inactive' OR wp_posts.post_status = 'acf-disabled')  ORDER BY wp_posts.post_date DESC LIMIT 0, 10",
            "posts": [
                {
                    "ID": 1339,
                    "post_author": "1",
                    "post_date": "2021-08-28 11:28:01",
                    "post_date_gmt": "2021-08-28 11:28:01",
                    "post_content": "",
                    "post_title": "This is a demo title",
                    "post_excerpt": "Test excerpt",
                    "post_status": "publish",
                    "comment_status": "open",
                    "ping_status": "open",
                    "post_password": "",
                    "post_name": "cool-title-2",
                    "to_ping": "",
                    "pinged": "",
                    "post_modified": "2021-09-05 11:56:47",
                    "post_modified_gmt": "2021-09-05 11:56:47",
                    "post_content_filtered": "",
                    "post_parent": 0,
                    "guid": "https:\/\/yourdomain.test\/?p=1339",
                    "menu_order": 0,
                    "post_type": "post",
                    "post_mime_type": "",
                    "comment_count": "0",
                    "filter": "raw"
                },
                {
                    "ID": 1295,
                    "post_author": "1",
                    "post_date": "2021-07-22 20:44:58",
                    "post_date_gmt": "2021-07-22 20:44:58",
                    "post_content": "test",
                    "post_title": "The second demo post",
                    "post_excerpt": "",
                    "post_status": "publish",
                    "comment_status": "open",
                    "ping_status": "open",
                    "post_password": "",
                    "post_name": "the-second-demo-post",
                    "to_ping": "",
                    "pinged": "",
                    "post_modified": "2021-07-22 20:44:58",
                    "post_modified_gmt": "2021-07-22 20:44:58",
                    "post_content_filtered": "",
                    "post_parent": 0,
                    "guid": "https:\/\/yourdomain.test\/?p=1295",
                    "menu_order": 0,
                    "post_type": "post",
                    "post_mime_type": "",
                    "comment_count": "0",
                    "filter": "raw"
                },
                {
                    "ID": 1047,
                    "post_author": "131",
                    "post_date": "2020-10-20 22:44:28",
                    "post_date_gmt": "2020-10-20 22:44:28",
                    "post_content": "This is more demo content.",
                    "post_title": "Another test",
                    "post_excerpt": "",
                    "post_status": "publish",
                    "comment_status": "open",
                    "ping_status": "open",
                    "post_password": "",
                    "post_name": "another-test-2",
                    "to_ping": "",
                    "pinged": "",
                    "post_modified": "2021-07-13 08:31:39",
                    "post_modified_gmt": "2021-07-13 08:31:39",
                    "post_content_filtered": "",
                    "post_parent": 0,
                    "guid": "https:\/\/yourdomain.test\/?p=1047",
                    "menu_order": 0,
                    "post_type": "post",
                    "post_mime_type": "",
                    "comment_count": "0",
                    "filter": "raw"
                }
            ],
            "post_count": 10,
            "current_post": -1,
            "in_the_loop": false,
            "post": {
                "ID": 1339,
                "post_author": "1",
                "post_date": "2021-08-28 11:28:01",
                "post_date_gmt": "2021-08-28 11:28:01",
                "post_content": "",
                "post_title": "Another demo post title",
                "post_excerpt": "Test excerpt",
                "post_status": "publish",
                "comment_status": "open",
                "ping_status": "open",
                "post_password": "",
                "post_name": "cool-title-2",
                "to_ping": "",
                "pinged": "",
                "post_modified": "2021-09-05 11:56:47",
                "post_modified_gmt": "2021-09-05 11:56:47",
                "post_content_filtered": "",
                "post_parent": 0,
                "guid": "https:\/\/yourdomain.test\/?p=1339",
                "menu_order": 0,
                "post_type": "post",
                "post_mime_type": "",
                "comment_count": "0",
                "filter": "raw"
            },
            "comment_count": 0,
            "current_comment": -1,
            "found_posts": 21,
            "max_num_pages": 3,
            "max_num_comment_pages": 0,
            "is_single": false,
            "is_preview": false,
            "is_page": false,
            "is_archive": false,
            "is_date": false,
            "is_year": false,
            "is_month": false,
            "is_day": false,
            "is_time": false,
            "is_author": false,
            "is_category": false,
            "is_tag": false,
            "is_tax": false,
            "is_search": false,
            "is_feed": false,
            "is_comment_feed": false,
            "is_trackback": false,
            "is_home": true,
            "is_privacy_policy": false,
            "is_404": false,
            "is_embed": false,
            "is_paged": false,
            "is_admin": false,
            "is_attachment": false,
            "is_singular": false,
            "is_robots": false,
            "is_favicon": false,
            "is_posts_page": false,
            "is_post_type_archive": false,
            "thumbnails_cached": false
        }
    }
}

Sign up for WP Webhooks news

Get the latest features, tutorials and promotions directly into zur inbox.