WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Sort on size (4 posts)

  1. cogmios
    Member
    Posted 4 years ago #

    I have a meta custom field "x" and a meta custom field "y".

    I want to build a page that shows posts sorted on size.

    I can do this on the "x" (by using 'meta_key'=>x and 'orderby'=>x)

    But how can i order it on x*y (AND sorting on the numeric value and not the text value) do I need to pre process it first?

  2. vtxyzzy
    Member
    Posted 4 years ago #

    You do not need to pre-process, but you will need to use filters. Here is some sample code to get you started:

    function mam_posts_join ($join) {
       global $mam_global_join;
       if ($mam_global_join) $join .= " $mam_global_join";
       return $join;
    }
    function mam_posts_orderby ($orderby) {
       global $mam_global_orderby;
       if ($mam_global_orderby) $orderby = $mam_global_orderby;
       return $orderby;
    }
    add_filter('posts_join','mam_posts_join');
    add_filter('posts_orderby','mam_posts_orderby');
    
    $mam_global_join = "JOIN $wpdb->postmeta metax ON (metax.post_id = $wpdb->posts.ID AND metax.meta_key = 'x')
       JOIN $wpdb->postmeta metay ON (metay.post_id = $wpdb->posts.ID AND metay.meta_key = 'y')";
    $mam_global_orderby = " metax.meta_value * metay.meta_value ASC";
    query_posts('caller_get_posts=1');
  3. cogmios
    Member
    Posted 4 years ago #

    Thanks!

  4. vtxyzzy
    Member
    Posted 4 years ago #

    You are welcome! Now, please use the dropdown at top right to mark this topic 'Resolved'.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags