0

I have a simple dropdown:

<select class="dropdown-class" name="sort-posts" id="sortbox" onchange="document.location.search=this.options[this.selectedIndex].value;">
        <option disabled selected >Select option</option>
        <option <?php if( isset($_GET["orderby"]) && trim($_GET["orderby"]) == 'date' && isset($_GET["order"]) && trim($_GET["order"]) == 'DESC' ){ echo 'selected'; } ?> value="?orderby=date&order=DESC">Newest</option>
        <option <?php if( isset($_GET["orderby"]) && trim($_GET["orderby"]) == 'date' && isset($_GET["order"]) && trim($_GET["order"]) == 'ASC' ){ echo 'selected'; } ?>  value="?orderby=date&order=ASC">Oldest</option>
        <option <?php if( isset($_GET["orderby"]) && trim($_GET["orderby"]) == 'title' && isset($_GET["order"]) && trim($_GET["order"]) == 'ASC' ){ echo 'selected'; } ?> value="?orderby=title&order=ASC">A-Z Asc</option>
        <option <?php if( isset($_GET["orderby"]) && trim($_GET["orderby"]) == 'title' && isset($_GET["order"]) && trim($_GET["order"]) == 'DESC' ){ echo 'selected'; } ?>  value="?orderby=title&order=DESC">A-Z Desc</option>
        <option <?php if( isset($_GET["orderby"]) && trim($_GET["orderby"]) == '50' && isset($_GET["order"]) && trim($_GET["order"]) == 'ASC' ){ echo 'selected'; } ?> value="?meta_key=budget&orderby=50&order=ASC">Price Under $50</option>
        <option <?php if( isset($_GET["orderby"]) && trim($_GET["orderby"]) == '199' && isset($_GET["order"]) && trim($_GET["order"]) == 'ASC' ){ echo 'selected'; } ?> value="?meta_key=budget&orderby=meta_value&meta_key=199&order=ASC">Price $50-199</option>
        <option <?php if( isset($_GET["orderby"]) && trim($_GET["orderby"]) == '200' && isset($_GET["order"]) && trim($_GET["order"]) == 'ASC' ){ echo 'selected'; } ?> value="?meta_key=budget&orderby=200&order=ASC">Price Over $200</option>
        </select>

and a WP_Query:

    $search_result = get_field('search_results', 'option', false);
$args4 = array(
    'post_type'      => array('post', 'tools', 'recipes', 'page'),
    'post__in'  => $search_result,
    'orderby' => get_query_var('orderby'), 
    'posts_per_page'         => '12',
    'post_status'    => 'publish',

);

I'm trying to get the query to SORT by a single value when selected, but still show the other posts after.

So for example, Any posts with the custom field (Checkbox) selecting: "50" would display all those posts, in alphabetical order, first - and then the remaining posts after, in alphabetical order.

Likewise, if the dropdown for 199 was selected, the first posts displayed would be those with 199 selected in the custom field checkbox, and then the remaining would display alphabetically after.

I've seen a lot of posts here, but they all seem to do the filtering in the WP_Query args, but I'm hoping to do it via the URL string if possible (because I have already got the dropdown setup and working for date/title).

I've tried:

?meta_key=budget&orderby=50&order=ASC

and

?meta_key=budget&orderby=meta_value&key=50&order=ASC

Thanks!

EDIT: I've also tried this:

function my_pre_get_posts( $wp_query ) {

// do not modify queries in the admin
if( is_admin() ) {
    return $wp_query;
}

    // allow the url to alter the query
    if( isset($_GET['budget']) ) {  
        $wp_query->set('orderby', 'meta_value_num');    
        $wp_query->set('meta_key', 'budget');
        $wp_query->set('meta_value', $_GET['budget']);  
    } 

// return
return $wp_query;
}

add_action('pre_get_posts', 'my_pre_get_posts');

and set the string to:

?budget=199&order=ASC

Doesn't work either :(

  • WordPress won't parse the meta_key in the URL query, unless if you use the query_vars filter to add meta_key to the public query vars. But then, you should know that the results (by default) will only include posts that have the specified metadata. Also, you should use orderby=meta_value_num if the meta value is numeric. – Sally CJ Aug 1 at 10:21
  • Thanks for your answer - any chance you could post a corrected version of my code so I can understand more fully what you're saying? – user2115227 Aug 1 at 10:38
  • Yes, I can post an answer, if you still need help with the query string thing. But looking at the question's edit, have you tried a lower priority (i.e. a greater number) - add_action('pre_get_posts', 'my_pre_get_posts', 99); ? – Sally CJ Aug 1 at 12:04
0

Try use get_query_var instead of $_GET and also increase priority

function my_pre_get_posts( $wp_query ) {

// do not modify queries in the admin
if( is_admin() ) {
    return $wp_query;
}

// allow the url to alter the query
if( isset(get_query_var( 'budget' )) ) {  
    $wp_query->set('meta_key', 'budget');
    $wp_query->set('meta_value', get_query_var( 'budget' ));  
} 

// return
return $wp_query;
}

add_action('pre_get_posts', 'my_pre_get_posts', 11);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.