SQL LIKE in WP_Query

时间:2016-11-28 作者:user107903

我正在尝试对我的CPT进行排序,以便可以搜索它们是否为复数。如果名称为digital,并且有人输入digitals,他们仍然会得到相同的结果。我试着用这个例子WP_Query with "post_title LIKE 'something%'"?. 我有它,它可以搜索;但是,如果他们将s放入,则不会返回任何结果。

这是我的职能。php:

add_filter( \'posts_where\', \'wp_enchanced_posts_where\', 10, 2 );
function wp_enchanced_posts_where( $where, &$wp_query )
{
    global $wpdb;
    if ( $wp_enchanced_title_search = $wp_query->get( \'wp_enchanced_title_search\' ) ) {
        $where .= \' AND \' . $wpdb->posts . \'.post_title LIKE \\\'\' . esc_sql( $wpdb->esc_like( $wp_enchanced_title_search ) ) . \'%\\\'\';
    }
    return $where;
这是我的搜索。php

<section class="list-grid">
      <ul>
    <?php  $args_resource = array(
    \'orderby\'     => \'title\',
    \'post_type\' => \'resource\',
    \'wp_enchanced_title_search\' => $resource
  );
$query_resource = new WP_Query($args_resource);?>
        <?php while ($query_resource->have_posts()) : $query_resource->the_post(); ?>
          <li>
            <div class="list-grid__toggle">
              <a><span><?php the_title(); ?></span></a>
              <ul class="list-grid__toggle__icons">
                <li><a class="icon-button icon-button--map" href="<?php the_field(\'map_link\'); ?>" target="_blank"><span>Map</span></a></li>
                <li><a class="icon-button icon-button--link" href="<?php the_field(\'link\'); ?>"><span>Link</span></a></li>
              </ul>
              <div class="list-grid__toggle__content">
                <div class="list-grid__toggle__content__info">
                  <?php the_field(\'description\'); ?>
                </div>
                <div class="list-grid__toggle__content__detail">
                  <?php the_field(\'additional_information\'); ?>
                </div>
              </div>
              <div class="list-grid__toggle__image" style="background-image: url(<?php $image = get_field(\'image\'); echo $image[\'sizes\'][\'resource\']; ?>);">
                <a class="list-grid__toggle__close"><span>Close</span></a>
              </div>
              <?php if (have_rows(\'related_links\')) : ?>
                <div class="list-grid__toggle__links">
                  <span>Related Links:</span>                
                  <ul>
                    <?php while (have_rows(\'related_links\')) : the_row(); ?>
                      <li><a href="<?php the_sub_field(\'link\'); ?>"<?php if (get_sub_field(\'target\')) : ?> target="<?php the_sub_field(\'target\'); ?>"<?php endif; ?>><?php the_sub_field(\'link_text\'); ?></a></li>
                    <?php endwhile; ?>
                  </ul>                
                </div>
              <?php endif; ?>
            </div>
          </li>
        <?php endwhile; ?>
      </ul>
    </section>
任何帮助都将不胜感激。

谢谢

亚伦

1 个回复
SO网友:Benoti

也许是一个简单的输入错误,

if ( $wp_enchanced_title_search = $wp_query->get( \'wp_enchanced_title_search\' ) 
这永远不会开火,你应该== 相反=

if ( $wp_enchanced_title_search == $wp_query->get( \'wp_enchanced_title_search\' ) 
希望它能起作用!