最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Compare $_POST returned values with term array

programmeradmin1浏览0评论

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
  • You reference $_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
  • Updated main topic – jam Commented Dec 9, 2020 at 20:41
Add a comment  | 

1 Answer 1

Reset to default 1

Just 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 of get_brands() allows you to specify exact ids of terms you require.
发布评论

评论列表(0)

  1. 暂无评论