如何将自定义域数据添加到搜索索引?

时间:2019-04-02 作者:Jaz

我需要使我的列表数据可搜索:

Complex, 2 column form example with a ton of data in it

专门针对课程提供者和讲师。我以前从未使用过元查询,有人知道我可以从哪里开始吗?

这是我当前的搜索表单:

形式:

<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( \'/\' ) ); ?>">
    <label>
        <span class="screen-reader-text"><?php esc_html_e( \'Search for:\', \'listify\' ); ?></span>
        <input type="search" class="search-field" placeholder="<?php esc_attr_e( \'Search\', \'listify\' ); ?>" value=""
        name="s" title="<?php echo esc_attr_e( \'Search for:\', \'listify\' ); ?>" />
    </label>
    <button type="submit" class="search-submit"></button>
</form>
搜索结果:
if ( isset( $_GET[\'listings\'] ) ) {
    return locate_template( array( \'archive-job_listing.php\' ), true );
}

global $style;

$blog_style = get_theme_mod( \'content-blog-style\', \'default\' );
$style      = \'grid-standard\' == $blog_style ? \'standard\' : \'cover\';
$sidebar    = \'none\' != esc_attr( listify_theme_mod( \'content-sidebar-position\', \'right\' ) ) && is_active_sidebar( \'widget-area-sidebar-1\' );

get_header(); ?>

<div <?php echo apply_filters( \'listify_cover\', \'page-cover\' ); ?>>
    <h1 class="page-title cover-wrapper"><?php printf( __( \'Search: %s\', \'listify\' ), get_search_query() ); ?></h1>
</div>

<div id="primary" class="container">
    <div class="row content-area">

        <?php if ( \'left\' == esc_attr( listify_theme_mod( \'content-sidebar-position\', \'right\' ) ) ) : ?>
            <?php get_sidebar(); ?>
        <?php endif; ?>

        <main id="main" class="site-main col-12 
        <?php
        if ( $sidebar ) :
            ?>
             col-sm-7 col-md-8<?php endif; ?>" role="main">

            <?php if ( \'default\' != $blog_style ) : ?>
            <div class="blog-archive blog-archive--grid 
                <?php
                if ( $sidebar ) :
                    ?>
                 blog-archive--has-sidebar<?php endif; ?>" data-columns>
                <?php add_filter( \'excerpt_length\', \'listify_short_excerpt_length\' ); ?>
            <?php endif; ?>

            <?php
            while ( have_posts() ) :
                the_post();

                if ( \'default\' == $blog_style ) :
                    get_template_part( \'content\' );
                else :
                    get_template_part( \'content\', \'recent-posts\' );
                endif;
            endwhile;
            ?>

            <?php if ( \'default\' != $blog_style ) : ?>
                <?php remove_filter( \'excerpt_length\', \'listify_short_excerpt_length\' ); ?>
                </div>
            <?php endif; ?>

            <?php get_template_part( \'content\', \'pagination\' ); ?>

        </main>

        <?php if ( \'right\' == esc_attr( get_theme_mod( \'content-sidebar-position\', \'right\' ) ) ) : ?>
            <?php get_sidebar(); ?>
        <?php endif; ?>

    </div>
</div>
功能:
global $listify_facetwp, $listify_widget_search_listings_instance;

// Make sure FacetWP Assets are loaded.
add_filter( \'facetwp_load_assets\', \'__return_true\' );

// Get widget instance.
$instance = $listify_widget_search_listings_instance;

// Active facets for this widgets.
$facets_list = isset( $instance[\'facets\'] ) ? array_map( \'trim\', explode( \',\', $instance[\'facets\'] ) ) : listify_theme_mod( \'listing-archive-facetwp-home\', array( \'keyword\', \'location\', \'category\' ) );

// Load active facets datas.
$facets  = array();
$_facets = $listify_facetwp->get_homepage_facets( $facets );
if ( is_array( $_facets ) && $_facets ) {
    foreach ( $_facets as $_facet ) {
        if ( in_array( $_facet[\'name\'], $facets_list ) ) {
            $facets[] = $_facet;
        }
    }
}
?>

<div class="job_search_form job_search_form--count-<?php echo absint( count( $facets ) ); ?>">
    <?php echo $listify_facetwp->template->output_facet_html( $facets ); // WPCS: XSS ok. ?>

    <div class="facetwp-submit">
        <input type="submit" value="<?php esc_attr_e( \'Search\', \'listify\' ); ?>" onclick="facetWpRedirect()" />
    </div>

    <div style="display: none;">
        <?php echo do_shortcode( \'[facetwp template="listings"]\' ); ?>
    </div>

</div>

<script>
function facetWpRedirect() {
    FWP.parse_facets();
    FWP.set_hash();
    window.location.href = \'<?php echo listify_get_listings_page_url(); ?>?\' + FWP.build_query_string();
}

(function( window, undefined ){
    var $ = window.jQuery;
    var document = window.document;

    $(document).on( \'keyup\', \'.facetwp-facet .facetwp-search\', function(e) {
        if ( e.keyCode == \'13\' ) {
            facetWpRedirect();
        }
    } );
})( window );
不确定这是否是您需要的正确信息。任何帮助都将不胜感激!

1 个回复
SO网友:jsmod

尝试将以下代码段添加到functions.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\' );
我从一个教程中学到了如何使用它https://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/, 它在WordPress 5.1.1安装上没有任何修改,对我来说非常适合。

然而,值得注意的是,我的网站使用的是核心WordPress帖子(带有自定义字段)。所以我没有用任何类型的自定义帖子类型来测试这一点。但本教程的介绍似乎暗示它也适用于自定义帖子类型。也许你可以试试,让我们知道它是否有效——以防其他人正在寻找一种方法来扩展默认的WordPress搜索,以包括自定义帖子类型的自定义字段。

相关推荐

Searching for content post

我需要的“wordpress搜索”引擎只能用于帖子的内容。我在寻找一些解决方案,但“wordpress搜索”引擎总是只按帖子标题显示结果。我可以修改什么,或者应该做什么来解决此问题?谢谢你的帮助!