如何在没有插件的情况下按不同的字段搜索WordPress?

时间:2019-04-23 作者:tpsReports

我正在构建自己的WordPress主题,我希望我的搜索查询能够通过标题、类别、标签和内容中的关键字来搜索帖子。到目前为止,我相信我的主题只是通过标题和内容中的关键字进行搜索。

如何确定当前正在搜索哪些字段?一旦我确定了这一点,我该如何修改我的搜索查询,以便它按上面列出的字段搜索帖子?

我尝试了类似于下面教程的内容。如果看到我主题中的代码可以帮助您回答,请告诉我您需要什么文件,我会发布它们。

<?php
/**
 * Extend WordPress search to include custom fields
 *
 * https://adambalee.com
 */

/**
 * Join posts and postmeta tables
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
 */
function cf_search_join( $join ) {
    global $wpdb;

    if ( is_search() ) {    
        $join .=\' LEFT JOIN \'.$wpdb->postmeta. \' ON \'. $wpdb->posts . \'.ID = \' . $wpdb->postmeta . \'.post_id \';
    }

    return $join;
}
add_filter(\'posts_join\', \'cf_search_join\' );

/**
 * Modify the search query with posts_where
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
 */
function cf_search_where( $where ) {
    global $pagenow, $wpdb;

    if ( is_search() ) {
        $where = preg_replace(
            "/\\(\\s*".$wpdb->posts.".post_title\\s+LIKE\\s*(\\\'[^\\\']+\\\')\\s*\\)/",
            "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
    }

    return $where;
}
add_filter( \'posts_where\', \'cf_search_where\' );

/**
 * Prevent duplicates
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct
 */
function cf_search_distinct( $where ) {
    global $wpdb;

    if ( is_search() ) {
        return "DISTINCT";
    }

    return $where;
}
add_filter( \'posts_distinct\', \'cf_search_distinct\' );

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

我找到了一个解决方案,允许我在搜索标题和内容的同时按标签和类别搜索帖子。这正是我想要的。

信用证:https://rfmeier.net/include-category-and-post-tag-names-in-the-wordpress-search/

add_filter( \'posts_join\', \'search_join\', 10, 2 );
/**
 * Joins the terms, term_relationship, and term_taxonomy tables.
 *
 * @global $wpdb
 *
 * @param string $join The sql JOIN clause.
 * @param object $query The current WP_Query instance.
 *
 * @return string $join
 */
function search_join( $join, $query ) {

    global $wpdb;

    if ( is_main_query() && is_search() ) {

        $join .= "
        LEFT JOIN
        (
            {$wpdb->term_relationships}
            INNER JOIN
                {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
            INNER JOIN
                {$wpdb->terms} ON {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id
        )
        ON {$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id ";

    }

    return $join;

}

add_filter( \'posts_where\', \'custom_posts_where\', 10, 2 );
/**
 * Callback for WordPress \'posts_where\' filter.
 *
 * Modify the where clause to include searches against a WordPress taxonomy.
 *
 * @global $wpdb
 *
 * @param string $where The where clause.
 * @param WP_Query $query The current WP_Query.
 *
 * @return string The where clause.
 */
function custom_posts_where( $where, $query ) {

    global $wpdb;

    if ( is_main_query() && is_search() ) {

        // get additional where clause for the user
        $user_where = custom_get_user_posts_where();

        $where .= " OR (
                        {$wpdb->term_taxonomy}.taxonomy IN( \'category\', \'post_tag\' )
                        AND
                        {$wpdb->terms}.name LIKE \'%" . esc_sql( get_query_var( \'s\' ) ) . "%\'
                        {$user_where}
                    )";

    }

    return $where;

}

function custom_get_user_posts_where() {

  global $wpdb;

  $user_id = get_current_user_id();
  $sql     = \'\';
  $status  = array( "\'publish\'" );

  if ( $user_id ) {

      $status[] = "\'private\'";

      $sql .= " AND {$wpdb->posts}.post_author = {$user_id}";

  }

  $sql .= " AND {$wpdb->posts}.post_status IN( " . implode( \',\', $status ) . " ) ";

  return $sql;

}

add_filter( \'posts_groupby\', \'custom_posts_groupby\', 10, 2 );
/**
 * Callback for WordPress \'posts_groupby\' filter.
 *
 * Set the GROUP BY clause to post IDs.
 *
 * @global $wpdb 
 *
 * @param string $groupby The GROUPBY caluse.
 * @param WP_Query $query The current WP_Query object.
 *
 * @return string The GROUPBY clause.
 */
function custom_posts_groupby( $groupby, $query ) {

  global $wpdb;

  if ( is_main_query() && is_search() ) {
      $groupby = "{$wpdb->posts}.ID";
  }

  return $groupby;

}

相关推荐

将架构添加到搜索结果页面的正文。已尝试使用IS_SEARCH

我正在尝试将架构标记添加到搜索结果页,并尝试使用以下代码:-function body_schema() { $schema = \'http://schema.org/\'; if(is_search()) { $type = \"SearchResultsPage\"; } else { $type = \'Blog\'; } echo \'itemscope itemtype=\"\' . $schema . $