foreeach loop meta_query
-
Hi,
I am looking for assistance on how to load and query (as filters) some custom fields using an array and foreeach loop for a front end search bar. Due to the need for 11 queries for this project (I’ve shortened the code fields) coding 11 if statements will crash my ajax_url and thus limit me to only 9 queries with very slow load times.
JS CODE BELLOW ******************************************************
$ = jQuery; var reportSearch = $("#Report"); var searchFORM = reportSearch.find("#reportSearch"); searchFORM.submit(function (e){ //stops refresh e.preventDefault(); //collects input from front end var data = { action : "report_search", address : reportSearch.find("#address").val(), unit : reportSearch.find("#unit").val(), } //console.log(data); $.ajax({ url : ajax_url, data : data, success : function (response) { console.log(response); //resets results viewing on new search reportSearch.find("#sList").empty(); for (var i = 0 ; i < response.length ; i++) { //console.log(response[i]); //lists the results var html = '<tr id="' + response[i].title + '"><td><a href="' + response[i].permalink + '" target="_blank">' + response[i].title + '</a></td><td>' + response[i].address +'</td><td>' + response[i].unit + '</td></tr>'; reportSearch.find("#sList").append(html); } } }); });
BELLOW IS MY PHP ATTEMPT **************************************************
add_action( 'wp_ajax_report_search' , 'report_search_callback' ); add_action( 'wp_ajax_nopriv_report_search' , 'report_search_callback' ); //REPORT AJAX CALLBACK function report_search_callback(){ if(is_user_logged_in()){ header('content-type: application/json'); //creates empty result var $result = array(); //loads post type $args = array( "post_type" => "Report", "post_per_page" => -1 ); //Creates variable to place front end input $address = ""; $unit = ""; $queryTest = [ "address", "unit", ]; foreach ($queryTest as $key => $value) { if (isset($_GET[$value])) { $args['meta_query'][] = array( 'key' => $key, //MUST BE CUSTOM FIELD NAME 'value' => $value, 'compare' => 'LIKE' ); } } //init query $report_query = new WP_Query( $args ); //loops to gather all posts while ( $report_query->have_posts() ) { $report_query->the_post(); //creates json file $result[] = array( "id" => get_the_ID(), "title" => get_the_title(), "address" => get_field('address'), "unit" => get_field('unit'), "permalink" => get_permalink() ); } echo json_encode($result); wp_die(); } else { echo "Please log in to continue"; } }
The PHP attempt code currently returns no records as I’m assuming it’s not searching for the correct key and value in the meta_query.
I am trying to avoid using 11 if statements as the ajax_url crashes and will only let me run up to 9 queries at a very slow loading time.
BELLOW IS A WORKING IF STATEMENT FOR THE CODE / THIS IS WHAT I HAVE 11 OF ATM *********
if (isset($_GET['address'])) { $address = sanitize_text_field( $_GET['address'] ); //JSON FILTER $args['meta_query'][] = array( 'key' => 'address', //MUST BE CUSTOM FIELD NAME 'value' => $address, 'compare' => 'LIKE' ); }
A rework of the foreEach meta_query might be needed and any help with this would be greatly appreciated!
- The topic ‘foreeach loop meta_query’ is closed to new replies.