如何使用WP_QUERY参数‘Like%..%’?

时间:2015-11-17 作者:cgee

我没有找到好的标题,但这是我的问题:

我有这个密码(works fine!):

  $resume_args = array();

  $resume_args[\'post_type\'] = \'resume\';
  $resume_args[\'posts_per_page\'] = 7;
  $resume_args[\'paged\'] = $paged;

  $resume_args[\'order\'] = \'DESC\';
  $resume_args[\'orderby\'] = \'ID\';

   $resume_search_cat = array(
      \'key\' => \'_resume_category\',
      \'value\' => str_replace(\'-\', \' \',$kategorie),
      \'compare\' => \'LIKE\'
    );

  /** Enable multiple meta query **/
  $resume_args[\'meta_query\'] = array(
    $resume_search_cat
  );

  $resumes = new WP_Query($resume_args);   
有了这个密码我就可以all 的职位post_type \'恢复\'。

但现在我想要post_type \'使用清晰的post\\u内容继续“”。

例如,一些post\\u内容:

post\\u内容=\'你好,这里是foo bar“

  • post\\u内容=”Hello World这是foo条形图“
  • post\\u内容=”您好stackexchange thisfoo在phpmyadmin中,我使用isbar“
  • "...WHERE post_content LIKE \'%foo%\'" //output: all three examples
    

    The question:

    How can I archive this in/with wordpress WP_Query?

    <我用谷歌搜索了很多,但没有找到任何解决方案

    1 个回复
    最合适的回答,由SO网友:cgee 整理而成

    在Wordpress codex中再次搜索后,我找到了解决方案:

    搜索参数根据关键字搜索显示帖子。

    s(字符串)-搜索关键字。基于关键字搜索显示帖子

    显示与搜索词“关键字”匹配的帖子:

    $查询=新的WP\\U查询(数组(\'s\'=>\'关键字\');

    http://codex.wordpress.org/wp_query#Search_Parameter

    以我为例:

      $resume_args = array();
    
      $resume_args[\'post_type\'] = \'resume\';
      $resume_args[\'posts_per_page\'] = 7;
      $resume_args[\'paged\'] = $paged;
    
      $resume_args[\'order\'] = \'DESC\';
      $resume_args[\'orderby\'] = \'ID\';
    
      $resume_args[\'s\'] = \'foo\';
    
       $resume_search_cat = array(
          \'key\' => \'_resume_category\',
          \'value\' => str_replace(\'-\', \' \',$kategorie),
          \'compare\' => \'LIKE\'
        );
    
      /** Enable multiple meta query **/
      $resume_args[\'meta_query\'] = array(
        $resume_search_cat
      );
    
      $resumes = new WP_Query($resume_args);