• 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)
  • 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.