How to filter WP attachments and draft, pending or private posts
By default, FacetWP only indexes, displays and filters “published” items, meaning posts that have their post_status
set to publish
.
This excludes attachments (like images, media, and PDFs), which are posts that have their post_status
automatically set to inherit
. It also excludes other possible post statuses, like draft
, pending
and private
.
Index attachments and draft, pending or private posts
By using the facetwp_indexer_query_args hook, it is possible to force FacetWP to index other post statuses than publish
. Two examples:
Index attachments
Add the folowing code to your (child) theme’s functions.php to make FacetWP index the attachment
post type for all facets. Make sure to click the re-index button after adding the code.
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
add_filter( 'facetwp_indexer_query_args', function( $args ) { $args['post_status'] = [ 'publish', 'inherit' ]; return $args; });
Note: this is not needed if you are using only a Search facet (and no other facets) on your page, because FacetWP does not index anything for Search facets. A Search facet will retrieve attachments as results as long as they are in the listing query.
Index pending posts
To index published and pending posts, add the following and re-index:
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
add_filter( 'facetwp_indexer_query_args', function( $args ) { $args['post_status'] = [ 'publish', 'pending' ]; return $args; });
Display attachments and draft, pending or private posts
Note that indexing attachments (or other post statuses) is not enough to display them in your post listing. To do this, the post statuses you have indexed need to be added to the post_status
array in your query arguments.
To display attachments in your query, besides adding inherit
to the post_status
argument array, you’ll also need to add the attachment
post type to the post_type
argument array. In a custom WP_Query this would like this:
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
<?php $args = [ 'post_type' => [ 'post', 'attachment' ], // retrieve posts and attachments 'post_status' => [ 'publish', 'inherit' ], // add 'inherit' post status for attachments 'posts_per_page' => 10, 'orderby' => [ 'title' => 'ASC' ], 'facetwp' => true ]; $my_query = new WP_Query( $args );
In a Listing Builder listing, the same query would look as follows. Note that attachments are called ‘Media’ in the Listing Builder settings:
Display attachments only
If you want to retrieve only attachments/media/PDFs, using post_type => attachment
and post_status => inherit
is enough:
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
<?php $args = [ 'post_type' => 'attachment', // retrieve attachments only 'post_status' => 'inherit', // add 'inherit' post status for attachments 'posts_per_page' => 10, 'orderby' => ['title' => 'ASC'], 'facetwp' => true ]; $my_query = new WP_Query( $args );
If you use any other facets than a Search facet, make sure to also let FacetWP index attachments.
Display only certain types of attachments
If you want to display only certain attachment types, you can use WP’s post_mime_type query argument.
The following example excludes all images, as defined by their MIME type in line 4:
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
// The following will only work if the query ONLY retrieves the 'attachment' post type and no others. // If you have multiple post types, use the 'posts_where' filter. $unsupported_mimes = array( 'image/jpeg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'image/x-icon' ); $all_mimes = get_allowed_mime_types(); $accepted_mimes = array_diff( $all_mimes, $unsupported_mimes ); $args = [ 'post_type' => [ 'attachment' ], // Use ONLY 'attachment' here 'post_status' => [ 'publish', 'inherit' ], // 'inherit' is needed for attachments 'posts_per_page' => 15, 'post_mime_type' => $accepted_mimes // only allow attachments that are not images ]; $my_query = new WP_Query( $args );
Alternatively, you can specify the desired MIME types directly. The following retrieves only pdf’s, Word- and Excel files:
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
// The following will only work if the query ONLY retrieves the 'attachment' post type and no others. // If you have multiple post types, use the 'posts_where' filter. $args = [ 'post_type' => [ 'attachment' ], // Use ONLY 'attachment' here 'post_status' => [ 'publish', 'inherit' ], // 'inherit' is needed for attachments 'posts_per_page' => 15, 'post_mime_type' => array( 'application/pdf', 'application/msword', 'application/vnd.ms-excel' ) // Specify the MIMI types to be in the query ]; $my_query = new WP_Query( $args );
Note that there is one caveat with the above approach using the post_mime_type
query argument: it will only work if the query retrieves only the attachment
post type, and not any others. If your query also retrieves other post types (posts, pages, custom post types), you’ll need to use the posts_where filter instead, as follows:
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
// The following will work if also OTHER post types than attachments in the query, like posts or pages. // See: https://wordpress.stackexchange.com/a/209720 add_filter('posts_where', function($where, $query) { global $wpdb; // Check if 'post_type' exists in the query and is an array if ( isset($query->query['post_type'] ) && is_array( $query->query['post_type'] )) { // Check if the query includes "attachment" in the post type argument // Exclude any posts with a post_mime_type containing "image" if ( in_array( 'attachment', $query->query['post_type'] ) ) { $where .= ' AND ' . $wpdb->posts . '.post_mime_type NOT LIKE \'image/%\''; } } return $where; }, 10, 2);
Search content within attachments/PDFs
To search for content within attachments/PDFs with a Search facet, you need a search plugin like SearchWP or Relevanssi, as the default WP search does not search attachments.
FacetWP has built-in support for SearchWP. For Relevanssi, you need to install the Relevanssi add-on. After installing SearchWP or the Relevanssi add-on, the Search facet will show new options for the relevant plugin’s search engines.
After selecting the right engine, you only need to retrieve attachments in your listing query. FacetWP does not index anything for Search facets, so if you only have a Search facet on your page, this is enough to enable searching within attachments/PDFs. However, when you have other facets on the page besides a Search facet, you’ll need to also force FacetWP to index attachments.
When you are using SearchWP, you can enable its Transfer Weight To Media Parent setting to transfer all relevance weight of an attachment to the attachment’s parent post (the post to which the attachment was uploaded). The effect of this is that the search will not return the attachments/PDFs themselves, but the parent post to which they have been attached.
With some additional custom code, it is also possible to Search PDFs without showing them directly in the listing, with only the PDFs’ parent posts showing up as results.