Overview

This filter lets you customize which posts and post types get indexed. You have access to the query arguments array that gets passed to WP_Query.

Parameters

  • $args | array | An array of WP_Query arguments (see below)

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

$args = [ 'post_type' => 'any', 'post_status' => 'publish', 'posts_per_page' => -1, 'fields' => 'ids', ];

Customize indexable post types

One of the things this hook lets you customize is the “indexable post types”: the post types that FacetWP indexes. This makes it possible to fix issues with some post types not being indexed, or to force FacetWP to index only specific post types, or to prevent indexing of specific post types.

How to check indexable post types.
How to check indexable post types.

To check which post types FacetWP currently indexes, click the “Show indexable post types” button in the settings, as shown in the image on the right. Changes you make with this hook will be reflected when you click the “Show indexable post types” button again.

Reducing the post types that FacetWP indexes can improve indexing speed. Even if your facets don’t apply to all post types that exist, FacetWP will still look up every post from every indexable post type, and check it against each facet to see if it needs to be indexed. So removing post types that are not used with facets can potentially speed up indexing.

Searchable and non-searchable post types

Post types can be “searchable” or “non-searchable”. Searchable post types will show up in search results, and non-searchable ones not. FacetWP by default only indexes searchable post types.

To be searchable, the exclude_from_search argument of the register_post_type() function must be set to false. You can do this directly in the function’s arguments, or with a hook.

But you may want to keep a post type non-searchable, to prevent these posts from showing up in the front-end WordPress search results, and still be able to index them. With this hook, you can force FacetWP to index and filter non-searchable post types.

Usage examples

Force indexing of non-searchable post types

FacetWP by default only indexes so-called “searchable” post types. This hook can be used to force FacetWP to index and filter post types that are “non-searchable”.

For example, to force the wprm_recipe post type to be indexed, even though it’s non-searchable, add the following code to your (child) theme’s functions.php:

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_type'][] = 'wprm_recipe'; // Force this post type to be indexed return $args; });

Index only specific post types

To index only specific post types, overwrite the $args['post_type'] array, as shown in the following snippet. This example indexes only the postand wprm_recipe post types.

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_type'] = ['post', 'wprm_recipe']; // Index only these post types return $args; });

Prevent indexing of searchable post types

To prevent indexing of a searchable post type, you can remove it, as shown in the following snippet. This example prevents indexing of pages:

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 ) { unset ( $args['post_type']['page'] ); // Prevent this post type from being indexed return $args; });

Index 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.

To force FacetWP to index the attachment post type for all facets, add the code following to your (child) theme’s functions.php, and then click the re-index button:

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; });

To index published and pending posts, use the following:

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; });

Note that indexing attachments (or other post statuses) is not enough to display them in your post listing. See this tutorial for more info about filtering and displaying attachments, and/or draft, pending, or private posts.

If you are using SearchWP, see our SearchWP page for additional options to search within attachments/PDFs.

More examples

See also

Last updated: November 22, 2024