Support

Account

Home Forums General Issues How to show related posts based on custom fields? Reply To: How to show related posts based on custom fields?

  • To do this you’re going to have to do a new WP_Query on these meta values. Something like this.

    
    $district = get_field('district');
    $price = get_field('price');
    $low = $price-50;
    $high = $price+50
    $args = array(
      'post_type' => 'your-post-type',
      'post_per_page' => 3,
      'meta_query' => array(
        array(
          'key' => 'district',
          'value' => $district
        ),
        array(
          'key' => 'price',
          'value' => $low,
          'compare' => '>='
          'type' => 'NUMERIC'
        ),
        array(
          'key' => 'price',
          'value' => $high,
          'compare' => '<='
          'type' => 'NUMERIC'
        )
      )
    );
    $query = new WP_Query($args);
    

    see https://codex.wordpress.org/Class_Reference/WP_Query

    also https://www.advancedcustomfields.com/resources/query-posts-custom-fields/