• Hey guys.

    Yesterday I was in need of customizing word press´ search query in order to allow multiple parameters, like terms, metaposts and etc.

    So, we have a left side bar containing many checkboxes that will be submitted to search template.

    All I have to do is a mechanism where I dynamically generate sql scripts based on parameters received by the form submit.
    We consider each parameter sent by GET and each one has this name as its key in postmeta.

    If you need to search on another keys, just need to customize the script.

    In search.php, I put in first lines :

    <?php
    
    $tables = array("$wpdb->posts", "$wpdb->term_relationships", "$wpdb->term_taxonomy", "$wpdb->terms");
    // DEFAULT TABLES, JOINED FOR CATEGORY
    
    $criterias = array(
                      "wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id",
                      "wp_terms.term_id = wp_term_taxonomy.term_id",
                      "post_status = 'publish'",
                      "post_type = 'ad_listing'",
                      "wp_term_relationships.object_id = wp_posts.id"
    		   );
    // DEFAULT JOINS BETWEENS CATEGORIES AND POSTS
    
    function add_to_sql_query($meta_name, $meta_value)
    {
    	global $tables, $criterias;
    
    	$tables[]    = "wp_postmeta $meta_name";
    	$criterias[] = "$meta_name.meta_key = '$meta_name'";
    	$criterias[] = "$meta_name.meta_value = '$meta_value'";
    	$criterias[] = "$meta_name.post_id = wp_posts.id";
    }
    
    $query_search = isset($_GET['s']) ? str_replace("|", "&", urldecode($_GET['s'])): '';
    
    parse_str(($query_search), $query_params); 
    
    $criterias[] = "wp_terms.slug like '" . ($query_params['slug']) . "%'"; //slug used only in terms table
    
    unset($query_params['slug']); //Don´t use slug for join tables
    
    foreach($query_params as $key => $value)
    {
    	add_to_sql_query(($key), ($value));
    }
    
    $sql  = "SELECT * FROM ";
    $sql .= implode(", ", $tables);
    $sql .= " WHERE ";
    $sql .= implode(" AND ", $criterias);
    
    $pageposts = $wpdb->get_results($sql, OBJECT);
    
    ?>
    

    And that´s it. SQL is being generated crossing each parameter from postmeta and searching for its values coming by GET(Querystring).

    In your search loop, just need to use iterations as follows:

    <?php if($pageposts) : ?>
      <?php global $post; ?>
      <?php $count = 1;?>
      <?php foreach ($pageposts as $post): ?>
         <?php setup_postdata($post); ?>
         <!-- html stuff.. You can use $post Object normally here -->
      <?php endforeach; ?>
    <?php endif; ?>
    

    And we are finished.

    Note: This is my first job (of many incoming) in WordPress.

Viewing 2 replies - 1 through 2 (of 2 total)
  • I think this code will work for a project of mine but I don’t get the line:

    $query_search = isset($_GET[‘s’]) ?… etc.

    What’s the ‘s’?

    Thread Starter cosmantra

    (@cosmantra)

    It´s the search term like this : var1=search_criteria|var2=another_criteria|var3=anotherone

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Searching with many parameters (PostMeta in my case)’ is closed to new replies.