What is wrong with this custom post type/custom field query?

Im trying to get related posts for actual post that user is seeing (custom post type named property)

At the moment it only gives me the related post from the same ‘pcategory’ which are the categories for the post type.

Read More

What I need is same post type category as it is now and also with the same post type custom field (‘location’).

Tried many ways but was not able to get it. What is wrong?

$querystr = "
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
    WHERE $wpdb->terms.term_id in (".$catId.")
AND $wpdb->term_taxonomy.taxonomy = 'pcategory'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'property'
AND $wpdb->postmeta.meta_key = 'location'

ORDER BY $wpdb->posts.ID LIMIT 0 ,$limit";

Related posts

Leave a Reply

1 comment