If you want to display a custom set of posts and custom post types (CPTs) in a Posts or Search module, you can create a filter to override query args in the
This code applies to custom queries only and doesn't work with the main query. If you want to override the main query, see this WordPress codex article.
The following code applies to the whole site, so wherever you use a Posts or Search module, the display will show the post types that you enumerated in your array.
Add the following code to the functions.php file in your child theme.
In Line 2, replace the post types in the array with the posts types that you want the custom query to display. In this example, the Posts or Search module will display three post types: standard posts, the posts belonging to a CPT called "another-post-type," and the posts from another CPT called "or-another-post-type".
In the Custom Query list on the Content tab in the Posts or Search module, you'll still see the list of post types (Posts Pages, and any CPTs that you have), but it won't matter which one you choose because this custom query is overriding that selection.
In actual use, you probably want to add a conditional to make this query apply only to certain pages, nodes, or custom IDs. Here are a couple of code examples.
Suppose you want the Posts and Search modules to display both standard posts and WooCommerce products on the front page of your site. Use this code instead of the code in the previous section.
The following code example displays both standard posts and WooCommerce products for a specific Posts module or Search module ID. Note that the conditional statement for the Search module is slightly different from that of the Posts module so we provide both code examples here.
- Set a custom ID on the Posts or Search module.
- Add the following code, replacing
idin Line 2 with the ID you assigned in the previous step.