0

I'm working on a project that I need to

  • We have x posts, 1 Sponsored (custom meta field), unlimited paid, unlimited free posts (these are types of posts)

  • We need to show 1 sponsored at top, unlimited paid after it and unlimited free after paid

  • We need to order all of them by distance to area user selected

  • Each post has location stored in database attached to it

Question #1:

Currently I made 3 different queries with 3 different $args:

 $args_sponsored = array(
                        'post_type' => array('le_place'),
                        'posts_per_page' => '1',
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'le_place_category',
                                'field' => 'slug',
                                'terms' => array($theCat)
                            )
                        ),
                        'meta_query' => array(
                            array(
                                'key' => 'entry_type',
                                'value' => 'sponsored',
                            )
                        )
                    );

                      $args_paid = array(
                        'post_type' => array('le_place'),
                        'posts_per_page' => -1,
                        'order' => 'DESC',
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'le_place_category',
                                'field' => 'slug',
                                'terms' => array($theCat)
                            )
                        ), 
                        'meta_query' => array(
                            array(
                                'key' => 'entry_type',
                                'value' => array('paid'),
                            )
                        )
                    );


                    $args_free = array(
                        'post_type' => array('le_place'),
                        'posts_per_page' => -1,
                        'order' => 'DESC',
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'le_place_category',
                                'field' => 'slug',
                                'terms' => array($theCat)
                            )
                        ), 
                        'meta_query' => array(
                            array(
                                'key' => 'entry_type',
                                'value' => array('free'),
                            )
                        )
                    );

and not sure this affect performance! is that a wrong method?

Question #2: How can I sort them by distance after getting all posts and then show them?

sortByDistance($postsPaid); // how can I get custom fields of post objects?

 $postsPaid = $queryPaid->get_posts();
                          sortByDistance($postsPaid); // how can I get custom fields of post objects? 


                            $placeLocation= get_field('location');
                               $longPlace =$placeLocation['lng'];

                         $latPlace = $placeLocation['lat'];



 function getDistance($lat2, $long2, $lat1, $long1) {
                        $R = 6378137; // Earth’s mean radius in meter
                        $dLat = rad($lat2 - $lat1);
                        $dLong = rad($long2 - $long1);

                      $a = sin($dLat / 2) * sin($dLat / 2) +
                                cos(rad($lat1)) * cos(rad($lat2)) *
                                sin($dLong / 2) * sin($dLong / 2);
                        $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
                        $d = $R * $c; // in meter

                        return intval((ceil($d) / 1000) * 0.621371); // returns the distance in Miles
                 };

1 Answer 1

0

OK, I added distance as custom field to allow me order them using wp_query orderby method!

 function    add_distance_as_customfield($posts){

       $longDest = $_GET['lng'];
                    $latDest = $_GET['ltd'];
      foreach ($posts as $post) {
           $placeLocation= get_field('location',$post->ID);
                               $longPlace =$placeLocation['lng'];

                         $latPlace = $placeLocation['lat'];
                         $distance=getDistance($latDest,$longDest,$latPlace,$longPlace);

                         if ( ! add_post_meta($post->ID, "distance", $distance,true ) ) { 
   update_post_meta( $post->ID, "distance", $distance );
}

      }
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.