【问题标题】:not getting any result by meta field in custom form自定义表单中的元字段没有得到任何结果
【发布时间】:2014-03-05 07:38:44
【问题描述】:

我有一个搜索表单,其中我的表单操作是这样的:

<form action="<?php echo esc_url(home_url('/searched-result')); ?>" method="post">

在搜索结果模板上我有这个查询,

<?php
// Only check these form fields (change the list as needed...)
$fields = array( 'custom_price', 'custom_beds', 'custom_garage');
foreach( $fields as $field ) {
    if( $_REQUEST[$field] != '' ) {
        // We have something to match, otherwise ignore the field...
        $meta_query[] = array(
            'key' => $field,
            'value' => $_REQUEST[$field],  // This is OK, WP_Query will sanitize input!
            'compare' => 'LIKE'
        );
    }
}

$args = array(
    'post_type' => 'property_post',
    'posts_per_page' => 1000,
    'meta_key' => 'SORTFIELD', // The name of the metakey to orderby
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'meta_query' => $meta_query,
);

$query = new WP_Query( $args );

if (have_posts()) : while (have_posts()) : the_post();

?>

<h1 class="title">

<?php the_title(); ?></h1>

【问题讨论】:

  • 您的问题还没有解决?
  • yesss........我在做正确的事情来在模板页面上显示搜索结果吗???
  • 是的,很高兴听到您的问题得到解决 :)
  • 不......我的意思是我的问题没有解决...... :(
  • 我可以为多个元值做一个搜索过滤器吗....@Bindiya Patoliya

标签: php wordpress search


【解决方案1】:

只有当所有 meta_values 都符合条件时,它才会返回结果。您必须添加一个称为关系的额外参数,并将其设置为“或”:示例:

$args = array(
'post_type' => 'product',
'meta_query' => array(
    'relation' => 'OR',
    array(
        'key' => 'color',
        'value' => 'blue',
        'compare' => 'NOT LIKE'
    ),
    array(
        'key' => 'price',
        'value' => array( 20, 100 ),
        'type' => 'numeric',
        'compare' => 'BETWEEN'
    )
)
);
$query = new WP_Query( $args );

【讨论】:

    【解决方案2】:

    未测试:

    $fields = array( 'custom_price', 'custom_beds', 'custom_garage');
    foreach( $fields as $field ) {
    if( isset($_REQUEST[$field]) && !empty($_REQUEST[$field]) ) {
           // We have something to match, otherwise ignore the field...
            $meta_query[] = array(
                'key' => $field,
                'value' => $_REQUEST[$field],  // This is OK, WP_Query will sanitize input!
                'compare' => 'LIKE'
                   );
               }
    }
    

    检查您是否在 $_REQUEST 中获得了价值。 希望这会有所帮助...

    【讨论】:

      【解决方案3】:
      $args = array(
          'post_type' => 'property_post',
          'posts_per_page' => 1000,
          'meta_key' => 'SORTFIELD', // The name of the metakey to orderby
          'orderby' => 'meta_value_num',
          'order' => 'ASC',
          'meta_query' => array(
              'relation' => 'OR',     
              $meta_query
          )
      );
      

      【讨论】:

      • 我想我是空的 $_REQUEST[$field],
      猜你喜欢
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-27
      • 2018-11-11
      • 1970-01-01
      • 2013-07-07
      相关资源
      最近更新 更多