Forums

Relevanssi - A Better Search
[resolved] Sorting results by "meta_key" values (10 posts)

  1. maduro-blanco
    Member
    Posted 11 months ago #

    Thanks for a great plugin.

    At the moment Relevanssi can be instructed to sort searches in order of post_date by using&orderby=post_date. However, I have a blog that uses two dating systems, namely, WordPress's own post_date, and a custom field for the original published date, which is always shown under the post's title even if the post_date has been updated. In most instances I can easily sort posts by this custom field value using &orderby=meta_value&meta_key=original_date. However, Relevanssi doesn't seem to recognise this.

    Is there currently a way around this problem, or will you be able to make Relevanssi sort by custom field values in the future?

  2. msaari
    Member
    Posted 11 months ago #

    Well, I didn't know this was possible, looks like this is a fairly new feature in WordPress. Why not, I think I can add it, I just have to think about how, as the sort is done using a simple object sort, I suppose I just need to add the meta values to the objects before sorting. I'll have to see how it works.

  3. maduro-blanco
    Member
    Posted 11 months ago #

    Thanks for that.

    I think the ability to sort results by meta_key (custom field) has been around for some time, and it is normally just a case of passing the query to query_posts. e.g.

    query_posts($query_string . '&orderby=meta_value&meta_key=original_date&order=desc')

    But for some reason Relevanssi bypasses my query_post submissions--perhaps it is simply a case of priority, and Relevanssi runs its own query, overriding any passed through function.php, or some such thing.

  4. maduro-blanco
    Member
    Posted 11 months ago #

    But then, it handles &orderby=post_date without any problems.

  5. msaari
    Member
    Posted 11 months ago #

    Relevanssi can't handle anything that it isn't explicitly told how to handle, as it doesn't use the WordPress query, but instead queries its own index.

  6. maduro-blanco
    Member
    Posted 11 months ago #

    I see what you're saying, and Relevanssi is only hard-coded to handle either sorting by "relevance" or post_date. Maybe some kind of filter hook could be inserted so that a custom SQL query could override the existing Relevanssi ORDER BY, e.g. by specifying a return along the lines of -

    WHERE $wpdb->postmeta.meta_key = 'original_date'
    ORDER BY $wpdb->postmeta.meta_value ASC|DESC

    or whatever the correct SQL is for the current purposes.

  7. msaari
    Member
    Posted 11 months ago #

    Except SQL has nothing to do with it, as Relevanssi is not sorting SQL results, but $post object arrays. So, in order to make meta value sort possible, I'll have to first add the meta values to the $post objects, then it's simple to add.

  8. maduro-blanco
    Member
    Posted 11 months ago #

    Ah, I understand now. I missed what you where saying earlier, that Relevanssi sorts its own list of $post objects internally. Well, I'll keep my fingers crossed and hope you can "sort it all out", as they say :)

  9. mattbarry
    Member
    Posted 4 months ago #

    Has this feature been added yet? Sorting by meta value would be perfect for my website :)

  10. msaari
    Member
    Posted 4 months ago #

    You can use relevanssi_hits_filter to sort the results any way you want, you can see an example using the meta values in this comment thread on Relevanssi.com.

Reply

You must log in to post.

About this Plugin

About this Topic

Tags