How to Customize Your WordPress Export with WP_Query
WP_Query is a WordPress class that allows you to fetch posts directly from the database. It's powerful enough for developers to perform extremely complex queries, but simple enough for a regular WordPress user to learn.
You can learn more about WP_Query and all of its parameters here: https://developer.wordpress.org/reference/classes/wp_query/
Rather than writing these WP_Query expressions by hand, you can use https://generatewp.com/wp_query/ to create them for you.
Using WP_Query in WP All Export
Go to All Export › New Export and select the WP_Query Results option. Then, select the query type from the drop-down. All of the custom post types available for export are added here automatically. In our default install, there are three options:
- Post Type Query
- User Query
- Comment Query
These options use WP_Query, WP_User_Query and WP_Comment_Query.
In this example, we're going to choose Post Type Query.
Building the Query
The most basic query would include a post type and a post status. For example, to get all published posts in the default WordPress "Posts" post type, the query would be:
"post_type" => "post", "post_status" => "publish"
Alternatively, you could export all Media Library items like images and attachments with this query:
"post_type" => "attachment", "post_status" => "inherit"
If you need to define multiple post types and multiple post statuses, you can use arrays. For example, you can get all pages and posts that are either pending, published, or drafts with the following query:
"post_type" => array( "post", "page" ),
"post_status" => array( "publish", "draft", "pending" )
Advanced WP_Query Examples
The WP_Query class has the ability to do custom field queries, taxonomy queries, author checks, and much more. There's no way that we can cover all of the possibilities in this article, but here, we'll cover some more advanced queries just to showcase the power of this feature.
Specific criteria for WooCommerce Products
For this example, we'll export a set of variable WooCommerce products with the following rules:
- Product must be
published
. - Stock value is higher than
0
. - Products are sorted by title in ascending order.
The query:
"post_type" => array( "product", "product_variation" ),
"post_status" => "publish",
"orderby" => "post_title",
"order" => "ASC",
"meta_query" => array( array(
"key" => "_stock",
"value" => 0,
"compare" => ">"
) )
Custom Post Type Search
For this example, we'll search all posts in the post type properties
added by a real estate theme. The criteria:
- Post type is
properties
- Status is
publish
- Author ID is
3
, which is a real estate agent - The property has the
For Sale
category assigned to it - Custom field
_featured
is set to1
, making it a featured property in the theme
The query:
"post_type" => "properties", "post_status" => "publish", "author" => 3, "tax_query" => array( array( "taxonomy" => "property_category", "field" => "slug", "terms" => "for-sale", ) ), "meta_query" => array( array( "key" => "featured", "value" => "1", "compare" => "=", ) )
Limit Posts to Export Example (WP Query + PHP Function)
If you need to limit the number of posts exported, you can use a custom PHP function inside of a WP_Query Results export.
To learn more about using PHP functions in exports, see How to Pass Exported WordPress Data Through PHP Functions.
You'll need to insert the following into either your child theme's functions.php file or a plugin like Code Snippets:
function example_get_those_ids($post_type = 'post', $number_of_posts = 5, $order = 'ASC', $post_status = 'publish', $order_by = 'ID') {
$query = get_posts(
array(
'post_type' => $post_type,
'numberposts' => $number_of_posts,
'order' => $order,
'orderby' => $order_by,
'post_status' => $post_status,
'fields' => 'ids'
)
);
return ( !empty($query) ) ? $query : array();
}
To call the function, you must pass five parameters:
- The post type (e.g., "post").
- The number of posts to export (e.g. "5").
- Whether they should be in ascending or descending order (e.g., "ASC" for the first X number of posts, "DESC" for the last X number of posts).
- The post status (e.g., "publish").
- How the posts should be ordered (e.g., ID, date, etc.)
For example, to export the last three pages that were published, you need to use the following:
"post_type" => "page",
"post_status" => "publish",
"post__in" => example_get_those_ids("page", "3", "DESC", "publish")
Or, to export the last five orders in WooCommerce that have a status of "Completed", you'd use the following:
"post_type" => "shop_order",
"post_status" => "wc_completed",
"post__in" => example_get_those_ids("shop_order", "5", "DESC", "wc_completed")
See:
Related Docs
Execute custom code to modify the export data on the fly.
Use filters to tell WP All Export which data should be exported specifically.
Client Mode allows you to give access to other user roles to run/download specific exports.