Skip to main content
deleted 1 character in body
Source Link
jdm2112
  • 3.7k
  • 1
  • 16
  • 23

Untested and semiosemi-pseudo code obviously. The idea is that a meta query is already an array of arrays, each field in your form is another nested array. If a value exists we push it onto the array.

<?php
$_location = $_GET['location'] != '' ? $_GET['location'] : '';
$_status = $_GET['status'] != '' ? $_GET['status'] : '';
// Etc for all form fields

$meta_query = array(); // Declare empty array


// Test each var for a value; if exists add to meta query array
if($_location) $meta_query[] = array( 'key' => 'wpcf-location-area', 'value' => $_location, 'compare' => 'LIKE' );
if($_status) $meta_query[] = array( 'key' => 'wpcf-property-status', 'value'   => $_status, 'compare' => 'LIKE' );

// Start the Query
$property_args = array(
  'post_type'     =>  'property-spaces',
  'posts_per_page' => -1,
  'meta_query'    =>  $meta_query
);

$propertySearchQuery = new WP_Query( $property_args );

Untested and semio-pseudo code obviously. The idea is that a meta query is already an array of arrays, each field in your form is another nested array. If a value exists we push it onto the array.

<?php
$_location = $_GET['location'] != '' ? $_GET['location'] : '';
$_status = $_GET['status'] != '' ? $_GET['status'] : '';
// Etc for all form fields

$meta_query = array(); // Declare empty array


// Test each var for a value; if exists add to meta query array
if($_location) $meta_query[] = array( 'key' => 'wpcf-location-area', 'value' => $_location, 'compare' => 'LIKE' );
if($_status) $meta_query[] = array( 'key' => 'wpcf-property-status', 'value'   => $_status, 'compare' => 'LIKE' );

// Start the Query
$property_args = array(
  'post_type'     =>  'property-spaces',
  'posts_per_page' => -1,
  'meta_query'    =>  $meta_query
);

$propertySearchQuery = new WP_Query( $property_args );

Untested and semi-pseudo code obviously. The idea is that a meta query is already an array of arrays, each field in your form is another nested array. If a value exists we push it onto the array.

<?php
$_location = $_GET['location'] != '' ? $_GET['location'] : '';
$_status = $_GET['status'] != '' ? $_GET['status'] : '';
// Etc for all form fields

$meta_query = array(); // Declare empty array


// Test each var for a value; if exists add to meta query array
if($_location) $meta_query[] = array( 'key' => 'wpcf-location-area', 'value' => $_location, 'compare' => 'LIKE' );
if($_status) $meta_query[] = array( 'key' => 'wpcf-property-status', 'value'   => $_status, 'compare' => 'LIKE' );

// Start the Query
$property_args = array(
  'post_type'     =>  'property-spaces',
  'posts_per_page' => -1,
  'meta_query'    =>  $meta_query
);

$propertySearchQuery = new WP_Query( $property_args );
Source Link
jdm2112
  • 3.7k
  • 1
  • 16
  • 23

Untested and semio-pseudo code obviously. The idea is that a meta query is already an array of arrays, each field in your form is another nested array. If a value exists we push it onto the array.

<?php
$_location = $_GET['location'] != '' ? $_GET['location'] : '';
$_status = $_GET['status'] != '' ? $_GET['status'] : '';
// Etc for all form fields

$meta_query = array(); // Declare empty array


// Test each var for a value; if exists add to meta query array
if($_location) $meta_query[] = array( 'key' => 'wpcf-location-area', 'value' => $_location, 'compare' => 'LIKE' );
if($_status) $meta_query[] = array( 'key' => 'wpcf-property-status', 'value'   => $_status, 'compare' => 'LIKE' );

// Start the Query
$property_args = array(
  'post_type'     =>  'property-spaces',
  'posts_per_page' => -1,
  'meta_query'    =>  $meta_query
);

$propertySearchQuery = new WP_Query( $property_args );