• Please Help me!
    I need to make search by several values of custom fields and title!
    How i can write this module? Searchform will be in sidebar of all pages, and how to transfer vars by GET nd what is handler of input form?
    At all, how i can do this?
    PLEASE HELP!
    may be someone have the same task and have some code….
    THANKS!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Read this http://www.wphub.com/use-meta_query-query-posts-postmeta/

    As for getting your GET variables, just use them as you would in plain PHP, for example if you want to search a price and a gender

    $_GET['price']
    $_GET['sex']

    However, you may want to look for a plugin doing what you want as coding may be complicated, depending on your programming level.

    Thread Starter Valentin5

    (@valentin5)

    Thank you very mach Veganist! Very useful advice! thanks!
    Would u like to help me any more?
    i write:
    <form>….. get params PRICE, SIZE…….. </form>

    now i make query:

    $args = array(
    ‘post_type’ => ‘product’,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘price’,
    ‘value’ => ‘15.00’,
    ‘compare’ => ‘<‘,
    ‘type’ => ‘NUMERIC’
    ),
    array(
    ‘key’ => ‘size’,
    ‘value’ => ‘S’,
    ‘compare’ => ‘=’,
    ‘type’ => ‘CHAR’
    ),
    )
    );
    $squery = new WP_Query( $args );

    HOW TO GO ON FOR show the posts in $squery???

    So, this code in sidebar of any page, visitor press SUBMIT and…..???

    hello !
    sorry i didn’t see your comment.
    So what you need to do now, is to add this code to your index.php or category.php file for example (whereever you want to results to be displayed) :

    $squery = new WP_Query($args);
    while ($squery->have_posts()) : $squery->the_post();
          get_template_part('content_post');
    endwhile;
    wp_reset_postdata();

    In my theme I have a template for posts, called content_post.php. It contains

    <article class="post" id="post-<?php the_ID(); ?>">
        <h2 class="post-title"><?php the_title(); ?></h2>
        <div class="post-content">
            <? the_content(); ?>
            <? echo get_post_meta($post->ID, 'price', true); ?>
        </div>
    </div>

    oh, and in your query you could replace

    'value' => '15.00',

    by

    'value' => $price,

    and put this before the query :

    if(isset($_GET['price')) {
       $price = $_GET['price']; // you also need to sanitize this somehow, there are several WP functions to do this
    }

    This is a working category template where i use ordering by newest/oldest/highest price first/lowest price first.

    <?php get_header(); ?>
    
      <?php if (have_posts()) : ?>
        <?
        $category = get_the_category();
        $current = get_query_var('cat');
        ?>
                <form method="get" action="" id="sort">
                    <select name="tri" onchange="location=this.options[this.selectedIndex].value;">
                        <option value="?o=newest"<? if(isset($_GET['o']) && $_GET['o'] == 'newest' || !isset($_GET['o'])) echo ' selected="selected"'; ?>>Recent</option>
                        <option value="?o=oldest"<? if(isset($_GET['o']) && $_GET['o'] == 'oldest') echo ' selected="selected"'; ?>>Oldest</option>
                        <option value="?o=price_asc"<? if(isset($_GET['o']) && $_GET['o'] == 'price_asc') echo ' selected="selected"'; ?>>price asc</option>
                        <option value="?o=price_desc"<? if(isset($_GET['o']) && $_GET['o'] == 'price_desc') echo ' selected="selected"'; ?>>price desc</option>
                    </select>
                </form>
                <? if (isset($_GET['o'])) {
                    $o = $_GET['o'];
                    if ($o == "newest") {
                        $args = array(
                            'posts_per_page' => '9',
                            'paged' => $paged,
                            'orderby'=>'date',
                            'order'=>'DESC',
                            'cat' => $current);
                    } else if ($o == "oldest") {
                        $args = array(
                            'posts_per_page' => '9',
                            'paged' => $paged,
                            'orderby'=>'date',
                            'order'=>'ASC',
                            'cat' => $current);
                    } else if ($o == "price_asc") {
                        $args = array(
                            'posts_per_page' => '9',
                            'cat' => $current,
                            'paged' => $paged,
                            'meta_key' => 'price',
                            'orderby' => 'meta_value_num meta_value', // meta_value_num is ok if your prices are in an english currency format
                            'order'=>'ASC',
                        );
                    } else if ($o == "price_desc") {
                        $args = array(
                            'posts_per_page' => '9',
                            'cat' => $current,
                            'paged' => $paged,
                            'meta_key' => 'price',
                            'orderby' => 'meta_value_num',
                            'order'=>'DESC',
                        );
                    } else {
                        // normal ordering by date
                        $args = array(
                            'posts_per_page' => '9',
                            'paged' => $paged,
                            'orderby'=>'date',
                            'order'=>'DESC',
                            'cat' => $current);
                    }       
    
                    $myposts = new WP_Query($args);
                    while ($myposts->have_posts()) : $myposts->the_post();
                        get_template_part('content_post');
                    endwhile;
                    wp_reset_postdata();
                } else {
                    // normal case
                    while (have_posts()) : the_post();
                        get_template_part('content_post');
                    endwhile;
                }
                ?>
                <nav class="navigation">
                     <span class="nav-previous"><?php previous_posts_link() ?></span>
                     <span class="nav-next"><?php next_posts_link() ?></span>
         </nav>
      <?php else : ?>
        <nav class="navigation">
            <span class="nav-previous"><?php previous_posts_link() ?></span>
            <span class="nav-next"><?php next_posts_link() ?></span>
         </nav>
      <?php endif; ?>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    I guess you can put the form whereever you want, you will probably need to update “action” though.

    BTW if you have a lot of posts, you might consider attacking the database directly. The code I posted here worked out, but as I have a DB with 4000 posts, each of them having 5 meta fields or more, it was very slow and needed lots of RAM. I finally used something like this :

    $myrows = $wpdb->get_results( "SELECT post_id FROM wp_postmeta WHERE meta_key = \"$meta_key\" AND meta_value LIKE \"%$meta_value%\"" );
    

    (backticks got stripped, so be sure to add them)

    If anybody is interested I can post the whole thing here.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How i can do my own search by my own psrsms? HELP!’ is closed to new replies.