Title: WordPress Database Query
Last modified: August 20, 2016

---

# WordPress Database Query

 *  [Weblinkscreations](https://wordpress.org/support/users/weblinkscreations/)
 * (@weblinkscreations)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/wordpress-database-query/)
 * Hi all,
 * Im using a theme that uses this code to get the related posts (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.
 * What I need is same post type category and also with the same post type custom
   field (‘location’).
 * Tried many ways but was not able to get it. Can someone help me on this?
 * Thanks in advance.
 * $query = “select $wpdb->posts.* from $wpdb->posts,$wpdb->terms,$wpdb->term_taxonomy,
   $wpdb->term_relationships where $wpdb->terms.term_id in (“.$catId.”) and $wpdb-
   >terms.term_id = $wpdb->term_taxonomy.term_id and $wpdb->term_taxonomy.taxonomy
   = ‘pcategory’ and $wpdb->term_taxonomy.term_taxonomy_id =
    $wpdb->term_relationships.
   term_taxonomy_id AND $wpdb->posts.post_status = ‘publish’ and $wpdb->term_relationships.
   object_id = $wpdb->posts.ID and $wpdb->posts.ID != “.$post->ID.” group by $wpdb-
   >posts.ID order by $wpdb->posts.ID LIMIT 0 ,$limit”;

The topic ‘WordPress Database Query’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 0 replies
 * 1 participant
 * Last reply from: [Weblinkscreations](https://wordpress.org/support/users/weblinkscreations/)
 * Last activity: [13 years, 7 months ago](https://wordpress.org/support/topic/wordpress-database-query/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
