Return to Display Queries on Your Site
Queries can be displayed using a custom shortcode that is generated for each query.
Shortcodes are little phrases enclosed in brackets that can be added to any post, page, or other content. (They can also be added in the HTML blocks of any Template.) They look like this:
[shortcode_name]On your site, WordPress will convert the shortcode into the actual thing for which it is serving as a placeholder — in this case, the query.
To use a query shortcode:
- Go to Creator > Queries.
- Locate the “Shortcodes” column among the list of queries.
- Highlight and copy the shortcode of the query you wish to display.
- Go to the page or other location where you wish to add the query.
- Paste or type in the query at the location you wish the query to appear.
- Save the page.
The query will now appear on that page.
Advanced
Shortcodes can sometimes make use of attributes, or options you can add to a shortcode to adjust its use. A shortcode with an attribute looks like this:
[shortcode_name attribute_name=”Attribute Value”]All query shortcodes have two default attributes:
- context – accepts “form” or “results”. Setting this means you are forcing only the query’s search form to display, or only the query’s results.
- results_page – accepts any URL. For shortcodes that are set as context=”form”, this URL tells the search form where to redirect to after submitting.
As an example, let’s say you want to display a search form on your homepage, but once the form is submitted, you want to send users to an interior page to view the results. In the homepage, you would add:
[sample_query context=”form” results_page=”https://www.example.com/my-interior-page”]In the interior page, you would add:
[sample_query context=”results”]These attributes are analogous to the widget options described here.
Developer Note
In addition to “context” and “results_page”, query shortcodes can accept certain arguments used by the WP_Query and WP_User_Query objects (depending on whether you are querying content or users) to limit the results that are returned.
For example, let’s say you have a query that lists all pages. At the bottom of some of your pages, you want to show a list of that page’s child pages. You could create multiple queries, or you could create one query and modify it by adding the following to the shortcode:
[sample_query parent=”67″] [sample_query parent=”151″]These shortcodes would only return results if the result had a parent page whose ID was 67 and 151, respectively.