• I am trying to sort my custom post type in wordpress while using WP_Query. Let’s say there is a custom field called **angle** ( 45, 90, 120, 180,.. etc ). I could sort the values directly. But in this case, I have to sort it based on sine or cosine of their values. Is there any way I could do that? I am a noob to wordpress and so can’t figure this out. This is what I tried, but it didn’t work for me.

    
        $paged 	= ( get_query_var('page') ) ? get_query_var('page') : 1;
        $angles	= array( 60, 90, 120 );
        $args	=	array(
        		"post_type"			=>	"tables",
        		"posts_per_page"	=>	15,
        		"paged"				=>	$paged,
        		"meta_query"		=>	array(
        			array(
        					'key'     => 'angle',
        					'value'   => sin(deg2rad($angles)),
        					'compare' => 'IN',
                            'type'    => 
        			)		
        		)	
        );
        
        $the_query = new WP_Query( $args );

    Thank you so much in advance.

    https://stackoverflow.com/questions/41648370/wordpress-is-that-possible-to-calculate-a-metas-value-and-sort-based-on-the-r

Viewing 1 replies (of 1 total)
  • Joey

    (@leglesslizard)

    Hi,

    I believe you’d have to do this after the query. WordPress has a lot of options for sorting by custom fields but it’s not that extensive. If you checkout the codex for WP_Query you can see the amount of ways you can sort by looking at the “orderby” parameter.
    Using the meta_value as you are above is looking to see what posts to show with the “compare” option. Again, this has a lot of options but nothing as specific as what you are after.
    If I was doing this I’d return all the posts of the correct post type that have the required meta attached (meta_key = field name) then sort them using usort as shown here

    Good luck,

    Joey

Viewing 1 replies (of 1 total)
  • The topic ‘Is that possible to calculate a meta’s value and sort based on the r’ is closed to new replies.