SELECT ite.item_id, ite.user_id, ite.category_id, ite.subcategory_id, ite.title, ite.des, ite.latitude, ite.longitude, ite.area, ite.city, ite.state, ite.country, ite.price, ite.reference_id, ite.time, ite.is_available, ite.sold_at, ite.is_status, ite.is_deleted, ite.user_type, ite.admin_id, ite.feature_first_place, ite.feature_first_page, ite.user_package_rel_id, ite.ads_updated_date, ite.reject_ad_reason, ite.thumb_id, ite.thumb_image, ite.create_at, ite.view_details_count, ite.view_phone_count , IF((SELECT count(*) FROM user_package_rel AS up WHERE up.user_id=ite.user_id AND datediff(DATE_ADD(up.created_at, INTERVAL up.visibility_day DAY), CURRENT_DATE()) ), 'Y', 'N') as is_featured_ad ,0 AS distance_in_km FROM items ite
JOIN categories as tmi ON ite.category_id=tmi.category_id
JOIN sub_categories as tsi ON ite.subcategory_id=tsi.subcategory_id
WHERE
(ite.is_available='Y' OR DATEDIFF(CURRENT_TIMESTAMP, ite.sold_at)<=2 )
AND ite.is_status='Y'
AND (tmi.category_id = 3
OR EXISTS (SELECT item_extra_field.* FROM item_extra_field
LEFT JOIN filter ON filter.filter_id=item_extra_field.filter_id
LEFT JOIN filter_value ON filter_value.filter_id=filter.filter_id AND item_extra_field.filter_value_id=filter_value.filter_value_id
WHERE item_extra_field.item_id = ite.item_id
AND filter.filter_label LIKE '%Organic%'
AND filter_value.value=1)
)
HAVING distance_in_km < 500
ORDER BY is_feature_ad ASC, feature_first_place ASC, distance_in_km ASC , ite.item_id DESC LIMIT 0, 14