Sort order by custom field not working properly

Forums › Forums › Search & Filter Pro › Sort order by custom field not working properly

Viewing 8 posts – 1 through 8 (of 8 total)
  • Anonymous
    #219023

    Hi,

    I have Search and Filter working on a custom post archive page. I have a sort field which sorts by a Meta Value which corresponds to a numeric ACF custom field.

    When you choose to sort by ascending or descending order with these fields, the order of the results changes but not in the order you’d expect (numerically).

    Could you take a look please?

    Thanks

    Anonymous
    #219024
    This reply has been marked as private.
    Trevor
    #219034
    This reply has been marked as private.
    Anonymous
    #219058
    This reply has been marked as private.
    Trevor
    #219067

    Could you deactivate the Post Types Order plugin and check?

    Anonymous
    #219088

    Okay, yeah that fixes it. So is there any way to make that plugin work with S&F sorting?

    Trevor
    #219094

    That plugin, and others like it, will impact sorting in our plugin as it acts later in the WordPress page creation process. Post Types Order does have a hook you can use though, to stop it working under conditions that you set. It is discussed here:

    https://wordpress.org/support/topic/to-avoid-plugin-conflict-how-to-disable-post-type-order-for-search-pages/

    Maybe like this (in your child theme functions.php file):

    add_filter('pto/posts_orderby/ignore', 'pto_ignore', 10, 3);
    function pto_ignore( $ignore, $orderBy, $query ) {
      global $searchandfilter;
    $sf_current_query = $searchandfilter->get(12345)->current_query();
    if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
    } else {
      $ignore = TRUE;
    }
    return $ignore; 
    }

    Where 12345 is changed to the ID of your search?

    I am not sure if this is much the same, but simpler/shorter:

    add_filter('pto/posts_orderby/ignore', 'pto_ignore', 10, 3);
    function pto_ignore( $ignore, $orderBy, $query ) {
      global $searchandfilter;
    $sf_current_query = $searchandfilter->get(12345)->current_query();
    if (!((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()==""))) $ignore = TRUE;
    return $ignore; 
    }
    Anonymous
    #219099

    Okay, brilliant. Will give it a go.

Viewing 8 posts – 1 through 8 (of 8 total)

Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.