-2

I am using this code to search products from a wordpress/woocommerce website.
My requirment is URL will be like "http://localhost/wp/?s=D34&post_type=product" While s=D34 is search string.

When user will search for a string. Data will be searched from All default fields+ product's custom filed. The below code work fine with http://localhost/wp/?s=D34 but when &post_type=product is concatenated with url then it say enter image description here

Code is given below

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 );
        $where .= " AND ($wpdb->posts.post_type = 'product') ";
    }

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

This is to prevent distinct values

  function cf_search_distinct( $where ) {
        global $wpdb;
if ( is_search() ) {
    return "DISTINCT"; //to prevent duplicates
}

return $where;

}
add_filter( 'posts_distinct', 'cf_search_distinct' );

So What modification is required?

URL http://localhost/wp/?orderby=price&post_type=product work fine

but what is wrong with http://localhost/wp/?s=D34&post_type=product

Wordpress version is sixteen and 4.4. Woocommerce Plugin is installed

In both cases Query is

Case 1: http://localhost/wp/?s=D34&post_type=product

Case 2: http://localhost/wp/?s=D34

AND wp_posts.post_type = 'product' AND (wp_posts.post_status = 'publish') AND (((wp_posts.post_title LIKE '%D34%') OR (wp_postmeta.meta_value LIKE '%D34%') OR (wp_posts.post_content LIKE '%D34%'))) AND (wp_posts.post_password = '') AND wp_posts.post_type IN ('post', 'page', 'attachment', 'product') AND (wp_posts.post_status = 'publish') AND (wp_posts.post_type = 'product')
2

If I understand you, what you are doing is wildly complicated. All you should need is a simple pre_get_posts filter:

add_filter( 'pre_get_posts', 'modified_pre_get_posts' ); 
function modified_pre_get_posts( $query ) { 
  if ( $query->is_search() ) { 
    $query->set( 'post_type', 'product' ); 
  } 
  return $query; 
}

Plus your filter that adds DISTINCT.

post_type is a query parameter rolled into Core. Passing an URL such as localhost/wp/?s=D34&post_type=product should by default limit your queries to the single named post type. You shouldn't have to do anything special to make that work. Your filter may in fact be causing trouble.

  • will it work with url like "localhost/wp/?s=D34&post_type=product" I just updated question a bit – Adnan Ali Dec 18 '15 at 17:23
  • Well it dont works. it simply say "No products were found matching your selection" – Adnan Ali Dec 18 '15 at 17:42
  • It shows all porducts when url is, localhost/wp/?post_type=product but do not work with "localhost/wp/?s=D34&post_type=product – Adnan Ali Dec 18 '15 at 17:48
  • Then the problem is with the s=D34 not the post type. That isn't matching. Where is that data stored? – s_ha_dum Dec 18 '15 at 17:49
  • One thing more is I also have to search from custom field that is the reason of that is to complicated – Adnan Ali Dec 18 '15 at 18:09
0

try this

function cf_search_where( $where ) {
    global $pagenow, $wpdb;

    // a little debugging will help you..
    //print_r ($where);
    //die();

    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 );
        $where .= " AND ($wpdb->posts.post_type = 'product') ";
    }

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

You just add hidden input type by put these code below to your searchform.php

<input type="hidden" value="post" name="product" id="product" />
  • This make the url like localhost/wp/?s=D34&product=post – Adnan Ali Dec 19 '15 at 12:38
  • <input type="hidden" value="product" name="post_type" id="product" /> This is correct form. but my porblem is not this. problem is when this type of url comes it is not treated as it should be. – Adnan Ali Dec 19 '15 at 12:41
  • Omg! I'm sorry for wrong solution for you. – Saichon Wongbua Dec 19 '15 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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