1

I need to make my listing data searchable:

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

Specifically for Course Provider and Instructor. I haven't used meta queries before, anyone know where I can start?

This is my current search form:

Form:

<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>

Search Results:

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>

Function:

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 );

Not sure if this is even the right information you need. Any help would be much appreciated!

2
  • Hello Jaz, and welcome to the site :) Are your custom fields used in the core WordPress posts or a custom post type?
    – jsmod
    Commented Apr 3, 2019 at 9:00
  • Thanks jsmod! It's a custom post type called ratings from wp job manager
    – Jaz
    Commented Apr 4, 2019 at 0:53

1 Answer 1

0

Try adding the following code snippet to your functions.php file:

/**
 * 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' );

I learned how to use this from a tutorial at https://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/, and it worked perfectly for me without any modifications on a WordPress 5.1.1 installation.

However, worth noting, my website was using the core WordPress posts (with custom fields). So I did not test this with custom post types of any kind. But the tutorial's introduction seems to imply that it will work for custom post types as well. Perhaps you could try it and let us know if it works - in case anyone else is looking for a way to extend the default WordPress search to include custom fields of custom post types.

1
  • Amazing, thank you!
    – Jaz
    Commented Jun 4, 2019 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.