0

I have this Custom Post Type that allows the client to enter their own Properties. On a single property page, in the sidebar I have 'Similar Properties' that shows 3 Similar Properties to the property page you're on.

So far the Similar Properties takes into account the location and status of the Property and only return Properties that are in the same location and has the same status (these two things are taxonomies so I use tax_query)

Although the Client also wants the Similar Properties to also only show Properties with the same number of beds as to that of the property you're on.

For Example: If I am on a Property Page with 3 beds then the Similar Properties should only return results that have the following:

  1. Has the same Status
  2. In the same Location
  3. Has only 3 beds

The beds get saved to the database with the meta_value = meta_bedroom

Is there anyway in which I make the query include the only Properties with the same amount of beds?

similar-property.php

<?php
global $post;

$properties = array(
    'post_type' => 'properties',
    'posts_per_page' => 3,
    'post__not_in' => array( $post->ID ),
    'orderby' => 'rand'
);

$tax_query = array();

/* Main Post Property Status */
$type_terms = get_the_terms( $post->ID, 'status' );
if ( !empty( $type_terms ) && is_array( $type_terms ) ) {
    $types_array = array();
    foreach( $type_terms as $type_term ) {
        $types_array[] = $type_term->term_id;
    }
    $tax_query[] = array(
        'taxonomy'  => 'status',
        'field'     => 'id',
        'terms'     => $types_array
    );
}

$tax_count = count( $tax_query );
if ( $tax_count > 1 ) {
    $tax_query['relation']  = 'OR'; // add Or relation if more than one
}
if ( $tax_count > 0 ) {
    $properties['tax_query'] = $tax_query;
}


/* Location of Properies */
$location_query = array();

$location_terms = get_the_terms($post->ID, 'location');
if ( !empty( $location_terms ) && is_array( $location_terms ) ) {
    $location_array = array();
    foreach( $location_terms as $location_term ) {
        $location_array[] = $location_term->term_id;
    }
    $location_query[] = array(
        'taxonomy'  => 'location',
        'field'     => 'id',
        'terms'     => $location_array
    );
}

$location_count = count( $location_query );
if( $location_count > 1 ) {
    $location_query['relation'] = 'OR';
}
if( $location_count > 0 ) {
    $properties['tax_query'] = $location_query;
}

$same_beds = get_post_meta($post->ID, 'meta_bedroom', true);

// Add meta_query here somewhere

if($same_beds == $post->ID) {
    $same_beds['meta_query']
}

$similar_properties = new WP_Query($properties);
?>

Update:

I have tried the following but still it does not return results with the same amount of beds

/* Same Beds */
$beds_query = array();

$key = 'meta_bedroom';
$same_beds = get_post_meta($post->ID, $key, true);

// Add meta_query here somewhere
if( !empty( $same_beds ) ) {
    $beds_query[] = array(
        'key'       => $key,
        'value'     => $same_beds,
        'compare'   => 'IN'
    );
}

$beds_count = count( $same_beds );
if( $beds_count > 0 ) {
    $properties['meta_query'] = $beds_query;
}
2
  • Simply just get the custom field value with get_post_meta() from the current single post, check if it has any value, and if so, append a meta_query to your existing query args Jun 7, 2016 at 13:13
  • @PieterGoosen Hmmm... I have updated my question to include the code for Similar Properties - it's basically it's own query. So how would I add the meta_query to make it work? As I am unsure how I could add this
    – Stephen
    Jun 7, 2016 at 13:26

1 Answer 1

1

As I have stated, you need to use the current post's ID and then return the desired meta value for the given key. This must then be passed to a meta_query.

Just a tip, instead of using the unreliable $post global, use the query object saved in $wp_the_query, this will be reliable in 99.99% of the time to return the single post object on a single post page

$post = sanitize_post( $GLOBALS['wp_the_query']->get_queried_object();

To get the meta value of then, you can do

$key = 'meta_bedroom';
$same_beds = get_post_meta(
    $post->ID, 
    $key,  
    true
);

Then we need to check if we have a value, and then append our meta_query

if ( $same_beds ) {
    $properties['meta_query'] = [        
        'key'   => $key,
        'value' => $same_beds
    ];
}
4
  • Sorry, there was some issues with my syntax, have corrected it Jun 7, 2016 at 16:11
  • Ah okay - I actually for the get_post_meta wrong as the $key was not meta_bedroom it was just bedroom I forgot I changed this a couple of days ago xD My bad your solutions works :) if you edit your syntax to make the last ) to a }
    – Stephen
    Jun 8, 2016 at 8:25
  • Glad to hear my solution worked. I have updated my syntax, thanks for pointing that out. If you are happy, feel free to accept my answer to mark this one as solved ;-) Jun 8, 2016 at 8:35
  • Yeah will do totally forgot xD
    – Stephen
    Jun 8, 2016 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.