• is there any way on how to create a custom orderby parameters in a url?
    for example site.com/?orderby=customparameters&order=ASC

    in that custom parameters, it will have a meta_key => price, meta_value => meta_value_num

    orderby=title and orderby=post_date is working. how can i add a custom parameters based on my settings not in wordpress default codex.

    I’m filtering a search post results. i just want to work in the url Thanks in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • I found it to be a little tricky, but this solution worked for me in the end.

    Let me know how you get on.

    You can always get the value of the parameters using $_GET[''] and pass the value in the WP_Query to get the desired results.

    Moderator bcworkz

    (@bcworkz)

    The reason the previous replies suggest WP_Query and pre_get_posts techniques instead of the URL parameter you asked for is you cannot normally pass a “meta_key” parameter in an URL, it needs to be set by PHP code. This is because “meta_key” is not a white listed URL parameter.

    You’re not going to be able to do what you want without some sort of code. While the previous suggestions will do the job, you could instead cause “meta_key” to be white listed with the following code added to functions.php:

    add_filter( "query_vars", "my_query_vars" );
    function my_query_vars( $vars ) {
        $vars[] = 'meta_key';
        return $vars;
    }

    With that in place, you can order by meta values with an URL similar to
    example.com/?meta_key=price&orderby=meta_value_num&order=ASC
    assuming your home page is a post listing. Most other post listings will work in a similar manner, just adjust the permalink portion accordingly.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘About orderby parameters’ is closed to new replies.