Forum Replies Created

Viewing 1 replies (of 1 total)
  • @mattifesto

    Caveats: This will exclude posts that don’t have a popularity custom field, but for people that know every post will have some value for a custom field, it’s a much easier and less fragile method of sorting by a custom field.

    I definitely can’t live with that, so I looked for another solution.
    Finally I asked a more “pro”-person, who came up with the following:

    add_filter('posts_join', 'new_join' );
    function new_join($pjoin){
    	if(is_category()){
    		global $wpdb;
    		$pjoin .= "LEFT JOIN (
    SELECT *
    FROM $wpdb->postmeta
    WHERE meta_key =  'add_year_value' ) AS metasort
    ON $wpdb->posts.ID = metasort.post_id";
    	}
    	return ($pjoin);
    }
    
     add_filter('posts_orderby', 'new_order' );
    function new_order( $orderby ){
    	global $wpdb;
    	if(is_category()){
    		$orderby = "metasort.meta_value ASC";
    	}
    
     	return $orderby;
    }

    Articles without the “add_year_value” -custom field also show up now.
    Hope it helps someone.

Viewing 1 replies (of 1 total)