SELECT DISTINCT tPost.id, tPost.country_code, tPost.category_id, tPost.post_type_id, tPost.title, tPost.price, tPost.city_id, tPost.featured, tPost.created_at, tPost.reviewed, tPost.verified_email, tPost.verified_phone, tPost.phone, tPost.contactseller, tPost.state, (tPost.price * 1) AS calculatedPrice, tPayment.package_id, tPackage.lft, (ST_Distance_Sphere(POINT(tPost.lon, tPost.lat), POINT(:longitude, :latitude)) / 1000) AS distance FROM posts AS tPost INNER JOIN categories AS tCategory ON tCategory.id=tPost.category_id AND tCategory.active=1 LEFT JOIN categories AS tParent ON tParent.id=tCategory.parent_id AND tParent.active=1 LEFT JOIN categories AS lCategory ON lCategory.translation_of=tCategory.id AND lCategory.translation_lang = :translationLang LEFT JOIN categories AS lParent ON lParent.translation_of=lCategory.id AND lParent.translation_lang = :translationLang LEFT JOIN (SELECT MAX(id) lid, post_id FROM payments WHERE active=1 GROUP BY post_id) latestPayment ON latestPayment.post_id=tPost.id AND tPost.featured=1 LEFT JOIN payments AS tPayment ON tPayment.id=latestPayment.lid LEFT JOIN packages AS tPackage ON tPackage.id=tPayment.package_id WHERE tPost.country_code = :countryCode AND (tPost.verified_email = 1 AND tPost.verified_phone = 1) AND tPost.archived != 1 AND tPost.deleted_at IS NULL AND tPost.reviewed = 1 GROUP BY tPost.id, tPayment.package_id, tPackage.lft HAVING distance <= 50 ORDER BY tPackage.lft DESC, tPost.price DESC, tPost.created_at DESC, distance ASC