Quantcast
Channel: Toolset » All Posts
Viewing all articles
Browse latest Browse all 20145

search in a specific custom field

$
0
0

I have read the treads http://wp-types.com/forums/topic/search-box-can-not-search-all-custom-fields/ and http://wp-types.com/forums/topic/using-wpv-filter-search-box-to-search-custom-field-content-in-a-view/ and tried to follow and use the code but it doesn´t work.

I have a view ( http://www.energi-miljo.se/sok-i-arkivet/ ) with 4 selectboxes and a search box. I want the search box to search in a specific custom field.

[wpv-filter-start hide="false"]
[wpv-filter-controls hide="true"]
<div class="svart-single"><h1>Sök i arkivet</h1></div>
<div class="bg-search-arkiv-block"><div class="dropcontent">[wpml-string context="wpv-views"][/wpml-string] 
	[wpv-control taxonomy="publikationstyp" url_param="wpvpublikationstyp" type="select" default_label="Innehållstyp" hide_empty="true"]
</div><div class="dropcontent">[wpml-string context="wpv-views"][/wpml-string] 
	[wpv-control taxonomy="amne" url_param="wpvamne" type="select" default_label="Ämnesområde" hide_empty="true"]</div><div class="dropcontent">
	[wpml-string context="wpv-views"][/wpml-string] 
	[wpv-control taxonomy="utgivare" url_param="wpv-utgivare" type="select" default_label="Utgivare" hide_empty="true"]
</div><div class="dropcontent">
	[wpml-string context="wpv-views"][/wpml-string] 
	[wpv-control taxonomy="utgivningsar" url_param="wpv-utgivningsar" type="select" default_label="Utgivningsår" hide_empty="true"]
</div><div class="sokord-arkiv">Fritextsök: [wpv-filter-search-box]</div><div class="sokknapp-arkiv">[wpv-filter-submit name="Sök"]</div>
</div>
[/wpv-filter-controls]
[wpv-filter-end]

I have added this the code in function.php:

add_filter( 'wpv_filter_query', 'hook_search_custom_fields', 10, 2 );
function hook_search_custom_fields( $query, $view_settings ) {
    if ( $view_settings['view_id'] == 7688 ) {
        add_filter( 'posts_search', 'search_custom_fields', 600, 2 );
    }
    return $query;
}
function search_custom_fields( $search, &$wp_query ) {
    global $wpdb;
    if (isset($_GET['wpv_post_search'])) {
        $s = esc_attr($_GET['wpv_post_search']);
        if($search != null && trim($search) != ''){ // <--added this statement
           $search = substr(trim($search), 0, -2) . " OR (wp_posts.ID IN (SELECT post_id FROM $wpdb->postmeta WHERE meta_key in ('wpcf-beskrivning-av-arkivinnehall') AND meta_value LIKE '%$s%'))))";
        }
    }
    remove_filter( 'posts_search', 'search_custom_fields', 600, 2 );
    return $search;
     
}

It does not work. If i put a specific searchterm in the custom field and test it do not show anything.
But if i put the same searchterm in the ordinarie textfield it show a result.

What am i doing wrong?
Must i change something in the view?
Can i see the fieldname in the list "custom fields controls"?
What do the numbers 10,2,600,2 in the code mean?

//Björ


Viewing all articles
Browse latest Browse all 20145

Trending Articles