• jirenu

    (@jirenu)


    How can i search only custom fields?
    code on my single.php
    <?php $values = get_post_custom_values("Genre"); echo $values[0] ?>
    Example: Drama, Thriller, War

    Now i wish something like:
    mywebsite.com/?genre=Action

    i tested this script on functions.php

    /**
     * Join posts and postmeta tables
     *
     * https://codex.www.ads-software.com/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
     *
     * https://codex.www.ads-software.com/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
     *
     * https://codex.www.ads-software.com/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 tried:
    mywebsite.com/?s=Action

    but it’s confused. appears too many titles and contents !!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Joy

    (@joyously)

    What you are describing is a taxonomy. You can use a plugin like Custom Post Type UI to create custom taxonomies, and then the URLs to each term are built-in searches.

    Moderator bcworkz

    (@bcworkz)

    Manipulating SQL queries by string manipulation (i.e. preg_replace()) is inherently weak and ought to be avoided where possible. If you are keen to code your own solution instead of using a plugin, I recommend using the “pre_get_posts” action. First you need to whitelist the desired “genre” query var through the “query_vars” filter. In “pre_get_posts”, if a “genre” query var is set there, get the value and use it to construct a “meta_query” argument.

    While your terms are currently meta values, as Joy points out, you are actually describing a taxonomy. Managing a set of genre taxonomy terms will work more consistently and be be easier to maintain than doing so with meta values. I recommend migrating your meta values to equivalent custom taxonomy terms. This can be done manually, but if there are a lot of posts to migrate a custom one time script could be written to migrate existing post meta to the new taxonomy. As a taxonomy, you don’t even need to set query vars and meta_query args in pre_get_posts. You can get all posts assigned a taxonomy term with an URL like example.info/genre/action/ without doing anything but creating the taxonomy.

    Thread Starter jirenu

    (@jirenu)

    I use “WP extended search” plugin, works perfect to get only posts with specific meta key.

    The problem is searching for posts title, maybe i need a new search to get posts by title.

    Who?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Search only custom fields’ is closed to new replies.