Creating Custom Export Query

This plugin creates or needs a list of post IDs for the export process. We can modify it and to create a custom export post IDs, for example export posts from specific post tags, multiple authors, meta keys, meta values and many others. To create a custom export query, there are three hooks that can be used:

  1. A filter hook wp_exporter_queries to create a list to the query drop-down. This filter passed default plugin queries as an array.
  2. An action hook wp_exporter_form to create your own custom form option that loads via WordPress AJAX. This action hook passed the selected query key.
  3. A filter hook wp_exporter_post_ids to generate the custom post IDs for the export. This filter passed default post IDs array and the form option arguments array.

We will create an example for exporting posts from specific post tag. We can add the code in your current theme functions.php. The first thing to do is adding our custom query to the drop down list.

add_filter( 'wp_exporter_queries', 'my_post_tag_export_query' );

function my_post_tag_export_query( $queries ) {
	$queries['my_post_tag'] = __( 'My Post Tag Query' );
	return $queries;
}

The function above will results a new query list like a screenshot below.

A new query list added

A new query list added

The next step is to create the posts query based on tag ID. We can use the WP_Query class to generate the post IDs or by using the get_posts function.

add_filter( 'wp_exporter_post_ids', 'my_exporter_post_ids', 1, 3 );

function my_exporter_post_ids( $post_ids, $query, $args ) {
	if ( 'my_post_tag' == $query ) {
		
		$post_ids = array(); // create empty array
		
		$posts = get_posts( 'tag_id=54' );
		
		foreach( $posts as $post )
			$post_ids[] = $post->ID;		
	}
	
	return $post_ids;
}

In the example above, we use post tag ID 54. You can change it to your post tag ID. At the end, by hitting the Download Export File, we will get all the posts from the post tag ID. Happy coding!