duanla4959 2015-07-08 09:58
浏览 59

Wordpress从标题(任何地方)获得结果,而不仅仅是第一个单词

Currently this is working code that is queried as standalone search.php.

I have an issue that it searches beginning of title and not anywhere in title what I exactly need.

I have a feeling that I'm close to solution and I tried couple of things and it is probably something that I'm missing in post $args..

global $wp_query;
$type = 'qa';
$args=array(
  'post_type' => $type,
  'post_status' => 'publish',
      'showposts'=>100,
      'orderby'=> 'menu_order',
      'order' => 'DESC'
);
$my_query = null;
$my_query = new WP_Query($args);
$data = array();
if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post();
      $title = get_the_title();
      $url = get_the_permalink();
      $data[$title] = $url;
  endwhile;
}

if(isset($_POST['latestQuery'])){
 $latestQuery = $_POST['latestQuery'];
 $latestQueryLength = strlen($latestQuery);
 $result = array();
 foreach($data as $name => $url){
   if (substr(strtolower($name),0,$latestQueryLength) == strtolower($latestQuery)){
       $result[$name] = $url;
   }
 }
 echo json_encode($result);
}

This is already working in local environment.

JavaScript queries search.php file (above) where it gets json array returned based on query... for example "cla.."

{"Classic something":"http:\/\/www.something\/qa2","Classic something else":"http:\/\/www.something\/qa"} 

So I have properly retuned two items that only have "Classic" mentioned in post title.

If I type "som" or "els" it doesn't return anything and it should match as well all "some" and "else" items.

I think it is not an issue with javascript code but with PHP code.

Thanks!

  • 写回答

1条回答 默认 最新

  • duanfen2349 2015-07-08 20:22
    关注

    Solved if would be helpful to someone in future.

    if(isset($_POST['latestQuery'])){
     $latestQuery = $_POST['latestQuery'];
    
    
    global $wp_query;
    $type = 'qa';
    $args=array(
      'post_type' => $type,
      'post_status' => 'publish',
      's' => "$latestQuery", // defined query
          'showposts'=>100,
          'orderby'=> 'menu_order',
          'order' => 'DESC',
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    $data = array();
    if( $my_query->have_posts() ) {
          while ($my_query->have_posts()) : $my_query->the_post();
          $title = get_the_title();
          $url = get_the_permalink();
          $data[$title] = $url;
      endwhile;
    }
    
     $result = array();
     foreach($data as $name => $url){ // removed if
           $result[$name] = $url;
     }
     echo json_encode($result);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器