skip to Main Content

I need to find some way to allow front-end filtering of the results of a query loop block. The ideal way to do this would be to use URL parameters to modify the query loop results, similar to the search block.

Is there an easy way to do this with a variation on the Query Loop block? Or am I going to have to make something entirely custom for this purpose? I’d love to use as much out-of-the-box code as possible.

2

Answers


  1. This was addressed on wordpress.stackexchange.com:

    To avoid modifying the main query, you might consider adding a custom query variable with PHP, and use ?qls=foo instead of ?s=foo.

    To target the corresponding query loop, add the text :query-loop-search into the search box of the query loop block in the editor.

    Then we could grab the value of the custom query variable ?qls=foo and set it as the search variable for the target query with a little more PHP.

    Source:
    https://wordpress.stackexchange.com/questions/407888/how-to-filter-query-loop-block-with-a-search-string-from-the-query-parameters

    Login or Signup to reply.
  2. You can extend the Query Loop block by creating a variation that specifies query parameters. Below is a simple block variation for the setting search parameter of the Query Loop:

    JS

    registerBlockVariation( 'core/query', {
        name: 'my-plugin/list-keyword',
        icon: 'smiley',
        attributes: {
            query: {
                search: 'keyword', // equivalent to /s?=keyword
                /* and optionally any other query settings.. */
            },
        },
        ...
    } ); 
    

    You can then add your Query block variation to any post/page/template.

    For the front-end, if you are editing the Search template for your theme, I would use the Button block to provide links for changing the search parameters, no block variation required, eg:

    Site Editor > Templates > Search
    Search template with buttons

    Depending on your end goal, the documentation on Extending the Query may be useful as well.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search