I am trying to compare Post values returned from AJAX with available taxonomy term_id but somehow I am out of luck. My $_POST['recvid'] values based on the user selection for example are '70=on&72=on&74=on' .
if( $brands = get_terms( array( 'taxonomy' => 'recipebrands' ) ) ) : //constructing array of available terms
$brands_terms = array();
foreach( $brands as $brand ) { //going thru all terms
if( ????? ) // trying to compare if POSTED values are within available term array
$brands_terms[] = $brand->slug; //if yes, then build the array of all slugs
}
endif;
Somehow I am stuck within the if clause and cannot figure out how to build the $brands_terms array
I tried
isset( $_POST['recvid'][$brand->term_id ] ) && $_POST['recvid'][$brand->term_id] == 'on')
but without luck.
Data from ajax contains 'recvid' variable:
jQuery.ajax({
url: my_custom_vars.ajax_url,
data: {
action: 'my_filter_function',
recvid: filter.serialize() // form data
},
Yes I know, should be learning much more about these things :)
I am trying to compare Post values returned from AJAX with available taxonomy term_id but somehow I am out of luck. My $_POST['recvid'] values based on the user selection for example are '70=on&72=on&74=on' .
if( $brands = get_terms( array( 'taxonomy' => 'recipebrands' ) ) ) : //constructing array of available terms
$brands_terms = array();
foreach( $brands as $brand ) { //going thru all terms
if( ????? ) // trying to compare if POSTED values are within available term array
$brands_terms[] = $brand->slug; //if yes, then build the array of all slugs
}
endif;
Somehow I am stuck within the if clause and cannot figure out how to build the $brands_terms array
I tried
isset( $_POST['recvid'][$brand->term_id ] ) && $_POST['recvid'][$brand->term_id] == 'on')
but without luck.
Data from ajax contains 'recvid' variable:
jQuery.ajax({
url: my_custom_vars.ajax_url,
data: {
action: 'my_filter_function',
recvid: filter.serialize() // form data
},
Yes I know, should be learning much more about these things :)
Share Improve this question edited Dec 9, 2020 at 20:43 jam asked Dec 9, 2020 at 19:24 jamjam 1732 gold badges3 silver badges15 bronze badges 2 |1 Answer
Reset to default 1Just check if $_POST contains key with id of correstponding term.
if (isset($_POST['recvid'][$term->term_id])) {
However, I would not loop through all the terms. get_terms
function has corresponding option. So, to receive all posted ids, you would do this (also note, that first parameter may be a string, no need for an array):
$brands = get_terms('recipebrands', [
'include' => array_filter(array_keys($_POST['recvid']), function($value) { return is_numeric($value); }),
]);
What above code does?
array_keys
returns $_POST array keys obviously.array_filter
filters out only numeric keys (see anonymous function).include
option ofget_brands()
allows you to specify exact ids of terms you require.
$_POST['recvid']
but I don't see that in your code. Can you add more to this for more context? – jdm2112 Commented Dec 9, 2020 at 20:34