Title: Query database
Last modified: August 20, 2016

---

# Query database

 *  [Weblinkscreations](https://wordpress.org/support/users/weblinkscreations/)
 * (@weblinkscreations)
 * [13 years, 7 months ago](https://wordpress.org/support/topic/query-database/)
 * 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”;

Viewing 1 replies (of 1 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/query-database/#post-3171594)
 * I do not have a way to test this, but I believe it is close to what you need.
 * Set the variable $location to the value of the custom field for the current post
   and then use this sql:
 *     ```
       $query = "select $wpdb->posts.*
       from $wpdb->posts,$wpdb->terms,$wpdb->term_taxonomy,
          $wpdb->term_relationships, $wpdb->postmeta
       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."
          and $wpdb->posts.ID = $wpdb->postmeta.post_id
          and $wpdb->postmeta.meta_key = 'location'
          and $wpdb->postmeta.meta_value = '$location'
       group by $wpdb->posts.ID
       order by $wpdb->posts.ID LIMIT 0 ,$limit";
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Query database’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [13 years, 6 months ago](https://wordpress.org/support/topic/query-database/#post-3171594)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
