Looper provider: Sort by one custom field, filter by another?

How can I display a list of posts, sorted by one custom field, and filtered by another? The following query string, which sorts by a custom field, works fine:

post_type=lunch_and_learn&orderby=meta_value&meta_key=lunch_and_learn_date&order=desc

But appending this does not work:

&meta_key=lunch_and_learn_status&meta_value=Scheduled

However, the following, complete query string – includes the filter clause but not the sort by clause – DOES work!

post_type=lunch_and_learn&meta_key=lunch_and_learn_status&meta_value=Scheduled

They just don’t work together. What am I doing wrong?

Hey, @cglobal! Thanks for writing in…if you’re trying to do anything with the Query String Provider that is even remotely complex, I would recommend reading through one of my responses to another user on how to craft complex custom queries here:

In there, you’ll see my technique for building out the query in a more readable format and then outputting it with http_build_query() somewhere on your site to get the less-readable but functional string. That way you should be able to read through the WP_Query documentation and format your query according to their setup and get the output you’re looking for. Doing custom taxonomy queries like this are a little more advanced and fall into the custom development realm and will need to be wired up on your end due to the unique nature of the setup, but that process for crafting your string in the reply above should help greatly in allowing you to try out different combinations and make sure that you’re always getting a proper string that isn’t missing a character here or there.

Cheers!

@kory I have repeatedly applied your method of creating a query string. It is very powerful, and one can flexibly achieve many things.
One problem: if one wants to change a single thing (e.g. querying a different category) one has to recreate the full string again.
I was wondering: is there a way to program a custom query (and use its hook), that then internally creates and uses a query string in the same way as it is used in your method?
Why would I want that? It would allow to use JSON parameters. So, instead if recreating quite complex query strings “by hand”, every time from scratch, similar and related queries could be implemented with a custom query and parameters.
Basically, I am asking for php code for a custom query, which internally generates this query string that hooks into the wp_query.
Thanks!

Hi @striata,

Instead of using the Query String you can use the Custom option where you can call any custom hooks. The custom hook method can be written into your child theme functions.php, and the parameter can be passed in the JSON format. Please find the example code that exactly returns the child page of the current page using the WP_QUERY.

add_filter('cs_looper_custom_childpage', function($result, $args) 
{
    $page_args = array(
        'post_type'      => 'page',
        'posts_per_page' => -1,
        'post_parent'    => get_the_ID(),
    );
    $cp =new WP_Query($page_args);
    $child_pages=$cp->posts;
    return $child_pages;
}, 10, 2);

You can also use the params option to send the parameter to the hook method, the example of the param and fetching the param in the code is given below

{ "parent_cat":"25"}  //JSON formatted parameter


$parent_cat=$args['parent_cat']; //this is the way you can get the parameter inside the method.

Please find the screenshot describing the options available for the above process.

test-new-Content-Pro (38)

Hope it helps.
Thanks

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.