Working with custom queries can be challenging, especially if you’re new to programming languages, and you need to implement several query methods for the post type you’re using.
CrocoBlock subscription already provides different widgets for displaying custom post types, such as Listing Grid widget in JetEngine, Smart Posts Tiles and Smart Posts List in JetBlog and Posts in JetElements.
However, while the Listing Grid widget for Elementor has robust query methods that are always at hand, you couldn’t use custom query types for JetBlog plugin widgets and for the custom posts displayed with JetElements plugin.
To solve this problem CrocoBlock now provides an easy-to use WP Query Generator tool, that allows creating custom queries to be easily applied for the dynamic posts feed.
Let’s find out how we can use the WP Query Generator for creating custom queries.
How the WP Query Generator Works
The generator is divided into several sections dedicated to different query parameters. When you set them, they become automatically added to the Generated Query code. The more parameters you’re using, the longer and more complex it becomes.
When you use “meta” or “tax_query”, you’ll be able to add several parameters at once and set the relation which will be applied to query the posts.
After everything is done you’ll have two options for using the code in the Generated Query block:
- Use JSON to get the generated query in the JSON format to use it in CrocoBlock Jet widgets. You’ll have to click “Copy to Clipboard” button to save the code.
- Open the Elementor editing page, click “Edit” on one of the widgets that provide ability to display posts (Smart Posts List, Smart Posts Tiles or Posts) and here locate “Custom Query” block in “Content > General”.
- Enable “Use Custom Query” option and paste the generated code in the “Set custom query” field.
The posts automatically will be queried using the set parameters.
Use PHP to get the query arguments array and use them in your work whenever you’re going to add WP Query to PHP.
To use the generated arguments array click “Copy to Clipboard” button and then paste the code to the needed .php file.
Setting General Parameters
When you first open WP Query Generator tool you’ll be automatically redirected to the General Parameters section. Here you’ll be able to set:
- The post type – here you’ll have to input the post type slug, e.g., “post”, “service”, etc.
- The post status – click on the field to select the status to query the posts using it (e.g., “publish”, “draft”, etc).
- The order – select ASC or DESC value to sort the posts using the ascending or the descending order.
- Order by – here you can choose the parameter which will be used to set the pots order (e.g., select “date” parameter and ACS order to show the posts from the oldest to the newest in the direct order).
- Posts per page – type in the number of posts you want to be displayed per 1 page (e.g., “5” to display only 5 posts from all the posts that are queried).
- Offset – use this parameter to hide some of the posts that were already displayed (e.g., “1” will hide the first post from the query from being displayed).
- Paged – here you have to define the number of page where you want to show the posts (in the case you want them to be displayed on this particular page).
- Page – here you can set the page number that is to be displayed on the static front page.
- Ignore sticky posts – enable this option to let the query work without considering the posts set as sticky.
Post & Page Parameters
In this section you’ll be able to pull the specific post using its ID, slug, title. You’ll also have the instruments to exclude or exclude pages using IDs, slug, or get the page which parent is in the list, or not in it.
There are also the fields where you can add information on the post IDs to identify the posts you want to retrieve.
Here you can define the author by ID or name to pull the posts that are published by one or several authors.
Category & Tag Parameters
In this section you’ll be able to include in the query the posts from the specific categories or tags (using category ID or slug) or exclude the posts by category ID, slug, or the specific tag.
E.g., to retrieve the posts that are related to Essays category but to exclude those which also have Non-fiction category, you’ll have to add the ID of “Essays” category in the Category In field and add “Non-fiction” category ID in the Category not in field.
In this case you’ll get only the posts that are included in “Essays” category and don’t fall under “Non-fiction” category at the same time (the posts that are categorized as “Essays” and “Non-fiction” at the same time, will be excluded).
This section allows using custom taxonomy terms to query the posts.
Here you can add several taxonomy types and use the specific taxonomy terms to retrieve the needed posts or exclude the ones you don’t want to be displayed.
Each taxonomy query (or tax_query) has the set of operators (IN, NOT IN, AND, EXISTS, NOT EXISTS) to define if the posts that have the specific terms should be included or excluded.
When you add more than one tax_query, you also can select the relation between the queries:
- AND – use it to query the posts that satisfy all the set taxonomy queries.
- OR – use it to query the posts that satisfy at least one of the queries.
Meta Query Parameters
In this section you can use several meta keys and query posts by meta key values (the option is especially useful when it comes to product post types and the prices).
- To create a new meta_query you’ll have to add an ID for the meta field (also called custom field) or get it from the database, and then input the value to use in the query method.
- In “Compare” block you’ll have the set of available operators that allow to query the posts that have the values in the defined custom field, that are equal, less or more than the set value, etc.
E.g., you can query the products which prices are higher or equal to $5 and exclude all the rest.
- If you add several meta query parameters, you’ll be able to define the relation between them using AND and OR operators ( they work the same as in tax_query).
Date Query Parameters
In this block you’ll be able to use the more advanced date queries to display posts that are published before or after the set date.
Feel free to add several parameters using “Add Item” button.
There is also “Relation” block with “AND” and “OR” operators to define the time interval of the posts that are to be queried.
Use this block to access additional query parameters and apply them for your posts feed.
- Has Password – here you can set, if you want to pull the posts with or without the password protection.
- Post Password – here you can define if you want to show only the posts that have the specific password.
- Comment count – to show only the posts that have the comments you can specify the comments count or define if the posts that are to be shown should have the number of comments more than the defined value (e.g., “>=10” will retrieve only the posts that have at least 10 comments, or even more).
- User permission – here you can define if the queried posts will be displayed only for the users that have certain permissions (in the user roles).
- Post mime type – here you can define the post mime types to specify the types of posts to query.
Creating custom queries with complex parameters won’t be a problem anymore.
Just keep using WP Query Generator tool and display the posts using any query type you want! It doesn’t matter if you’re working with Elementor or just looking for a way to simplify your work with PHP, the generator will create the query using your custom arguments, values and operators in a blink of an eye!