dtf24224 2014-09-25 07:26
浏览 69

WordPress:meta_query和数组

So, I have a search form, that returns custom posts with wp_query. Now I need to find posts with a custom field and specific value: Lets say I have multiple checkboxes named catagories[]. So when I save them it makes a serialized array in db. Now I need to search in the front end posts with lets say value 11 in it. Array looks like that: a:3:{i:1;s:2:"11";i:2;s:2:"33";i:3;s:2:"33";}. So here comes the problem, how can I retrieve information from this serialized array to find all posts with value 11. My code is:

$args = array(
         'post_type'=>'paibcresume',
         'posts_per_page' => 10,
         'paged' => $paged,
         'meta_query' => array(),
            'tax_query' => array(),
         'orderby' => 'date',
            'meta_key' => '',
         'order' => 'DESC'
      );


      // BASIC SEARCH FORM

      // Search by category
      // rbsercategories
      // rbwwcategoryhidden
      if (isset($_GET['rbsercategories']) && !empty($_GET['rbsercategories'])) {
         $args['meta_query'][] = array(
            'key'     => 'rbwwcategoryhidden',
            'value'   => $_GET['rbsercategories'],
            'compare' => 'IN'
         );
      }

      $the_query = new WP_Query( $args );
      while ($the_query->have_posts() ) : $the_query->the_post();

This code works if values in data base are not a serialized array, just simple strings, but doesn't work with arrays so what do I have to do?

  • 写回答

1条回答 默认 最新

  • dqfsbvd43312 2014-09-25 09:18
    关注

    Ok, I didn't find the way to search thru serialized arrays, but I found the way to work around it.

    So I didn't change any fields in admin, instead I added new with a loop. So I have this fields named rbwwcategoryhidden[], that create this array. I unserialized that array and created new fields for each value:

    $wwCategory = get_post_custom_values('rbwwcategoryhidden');
    
    foreach($wwCategory as $wwCategoryValue){$wwCategoryUnser = unserialize($wwCategoryValue);}
    
    $rbWwCatSearchCounter='0';
    
    foreach($wwCategoryUnser as $catId){
      $rbWwCatSearchCounter++;
      echo '<input type="text" name="rbwwcatforsearch'.$rbWwCatSearchCounter.'" id="rbwwcatforsearch'.$rbWwCatSearchCounter.'" value="'.$catId.'">';
    }
    

    So now I got new fields, I personaly have a limit of three fields (user can only check three checkboxes): rbwwcatforsearch1, rbwwcatforsearch2, rbwwcatforsearch3. And I added value to each field from that array: 11, 33 and 33.

    And now I just changed the meta_query code in the front end, and it looks like that:

    // Search by category
    if (isset($_GET['rbsercategories']) && !empty($_GET['rbsercategories'])) {
      $args['meta_query'][] = array(
        'key'     => array('rbwwcatforsearch1','rbwwcatforsearch2','rbwwcatforsearch3'),
        'value'   => (isset($_GET['rbsercategories'])?$_GET['rbsercategories']:array()),
        'compare' => 'IN'
      );
    }
    

    And it works just perfect :)

    评论

报告相同问题?

悬赏问题

  • ¥15 vue2(标签-chrome|关键词-浏览器兼容)
  • ¥15 python网络流自动生成系统 医学领域
  • ¥15 sql查询仓库里都有什么
  • ¥15 代码的修改,添加和运行完善
  • ¥15 krpano-场景分组和自定义地图分组
  • ¥15 lammps Gpu加速出错
  • ¥15 关于PLUS模型中kapaa值的问题
  • ¥15 关于博途V17进行仿真时无法建立连接问题
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器