• Someone kindly helped me with some custom code for a business listing website I am building.

    There are featured listings and standard listings. The ACF True/False field ‘business_featured_business’ is the way they are differentiated.

    I want featured businesses to list first then standard listings and the businesses to be in alphabetical order of post title within that.

    Because I am using front end forms to allow people to create their listing, the form for standard listings doesn’t have the ‘business_featured_business’ field in it. This results in no post meta entry being created when the form is submitted. (I can’t find a way to set the meta value to 0 automatically)

    The problem is that the code I have for sorting on the archive page isn’t working. It successfully displays my featured businesses first – and seems to display those in alphabetical order. But the businesses that aren’t featured are sorting in the default WP descending post date order.

    Can anyone help me to adjust the code below to get the sorting right i.e.

    Featured
    Business A
    Business D
    Standard
    Business B
    Business C

    Many thanks in advance.

    add_action('pre_get_posts', function($query) {
    if ($query->is_post_type_archive('business-listings') && $query->is_main_query()) {
    $meta_query = array(
    'relation' => 'OR',
    array(
    'key' => 'business_featured_business',
    'compare' => 'EXISTS',
    ),
    array(
    'key' => 'business_featured_business',
    'compare' => 'NOT EXISTS',
    ),
    );
    $query->set('meta_query', $meta_query);
    $query->set('orderby', array('meta_value_num' => 'ASC', 'title' => 'ASC'));
    }
    });
    • This topic was modified 1 year, 6 months ago by dustycat.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    I checked the resulting SQL from your code and the orderby portion is
    ORDER BY wp_postmeta.meta_value+0 ASC, wp_posts.post_title ASC
    just as you asked. If you’re getting different ordering, perhaps some other code is overriding yours? Try adding a large priority arg to the add_action() call after the closure function to help ensure your code has the final say.

    If you still have trouble, examine the resulting SQL with something like
    echo $query->request;
    The output can be hard to find in some cases. View the page’s source HTML. The SQL should be at or near the top.

    Thread Starter dustycat

    (@dustycat)

    I’ve viewed my source code and as you say, the result seems to be as you got. I’ve checked the FacetWP facets that I have running on the page and none of them are sorting (only filtering) and the Loop Grid widget is set to “current query” so it isn’t doing anything other than outputting the results using the loop item template.

    I’m wondering if it’s something to do with the way the meta-query is structured especially because it is separating out “posts with a value” and “posts without a value”.

    Would then the meta_key_num only be grabbing the “posts with a value” posts to then sort – with the rest left to the default WP archive sorting of reverse date?

    Is there a better way of achieving what I need?

    Moderator bcworkz

    (@bcworkz)

    “meta_query” is only used to construct a WHERE clause. As you’ve used it it has no practical bearing on how ordering in managed besides specifying the meta_key to use. There is an alternative way of structuring “meta_query” to allow ordering by certain specific sub-array values. As you’ve used it though, all that matters in ordering is the numeric value of any meta value keyed ‘business_featured_business’.

    What you have is fine if you need to include records that do not have a postmeta record for ‘business_featured_business’ at all. If all records have a ‘business_featured_business’ field, but maybe not a value, there’s a simpler query possible.

    Records without a corresponding ‘business_featured_business’ key or value will naturally fall to the bottom of the list and I’d expect them to be secondarily ordered by title. If you don’t get expected results, I suspect some other code is further altering the query.

    Are ‘business_featured_business’ values really numeric? If not, you’ll get unexpected ordering because most alphabetic values will resolve to the same number. If values are not numeric, you’d want
    $query->set('orderby', array('meta_value' => 'ASC', 'title' => 'ASC'));

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Orderby Custom Field then Post Title not working’ is closed to new replies.