Support

Account

Home Forums General Issues Sort by custom field filtering instead of sorting

Solved

Sort by custom field filtering instead of sorting

  • I followed the instructions to sort by a custom post. What I’m doing is almost identical to the example in the documentation.

    I use the following arguments to display a list of properties sorted by a true/false field called “is_featured”:

    $args = array (
        'post_type'              => 'properties',
        'posts_per_page'         => '-1',
        'meta_key'               => 'is_featured',
        'orderby'                => 'meta_value',
        'order'                  => 'DESC',
        'tax_query' => array(
            array(
                'taxonomy' => 'destination',
                'field' => 'id',
                'terms' => $destination,
            )
        )
    )

    However, in the front end only the properties with the “is_featured” field set as “true” are displayed. But what I want is the featured properties displayed first and then ALL the rest. What could be the problem?

    Thank you.

  • Have you tried debugging this by removing the “tax_query” and then removing the “meta_key” and “orderby” to see if the query is returning all the properties you’re expecting?

  • I just found out what the problem was. All properties that had never been edited before the addition of the “is_featured” field had no value in that field (not even “false” value) that’s why they weren’t being displayed. To fix it I had tu run a process that added the “false” value to all properties that had no value set.

    If you ask me, I think WordPress should still display at the end all posts that don’t have a value set in the custom field, but it looks like it just does not work that way.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Sort by custom field filtering instead of sorting’ is closed to new replies.