I have a problem showing results in Wordpress with WP_Query. Problem is simple but as far I'm not programer I wound it difficult.

So, I have custom field with values like 1-1-1-1 and 2-1-1-1-1 for example. And when I search for example 1-1-1-1 it gives me also second result and thats what I dont want to. I only want exact match to show in my search results. Can anybody help me out?


    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        global $wp_query;

        $query = new WP_Query( 'meta_key=wpcf-resitevtags' );
        $query = $wp_query->query;
        if (!is_array($query)) parse_str($query, $query); 
        $custom_query = new WP_Query(array_merge($query, array('posts_per_page' => $gantry->get('search-count'), 'meta_key' => 'wpcf-resitevtags', 'posts_per_page' => 22,  'paged' => $paged)));

        <?php if($custom_query->have_posts()) : ?>

        <?php if($gantry->get('search-page-title')) : 

        <?php endif; ?>

        <?php while ($custom_query->have_posts()) : $custom_query->the_post(); ?>


        //$title    = strip_tags(get_the_title());
        $content    = strip_tags(strip_shortcodes(get_the_content(false)));
        $excerpt    = strip_tags(strip_shortcodes(get_the_excerpt()));
        $keys       = explode(" ",$s);
        //$title    = preg_replace('/('.implode('|', $keys) .')/iu', '<span class="search-excerpt">\0</span>', $title);
        //$content  = preg_replace('/('.implode('|', $keys) .')/iu', '<span class="search-excerpt">\0</span>', $content);
        //$excerpt  = preg_replace('/('.implode('|', $keys) .')/iu', '<span class="search-excerpt">\0</span>', $excerpt);

  • 1
    I'm guessing that you're using compare LIKE when you need = - post your code so we can help! – TheDeadMedic Aug 6 '12 at 11:16
  • Compare Like? No. Here is my code. Here I can tell you more, that search string is coming from Flash. I edited post and add code that is showing my results. – Jure Mali Aug 6 '12 at 11:56
  • Need to see the code relevant to your question - what's the content of $query? – TheDeadMedic Aug 6 '12 at 12:05
  • Aha. If I understood your question content is what I've written in my post. 1-1-1-1, 2-1-1-1-1 and some common code. This code is needed if I want to show results that depends on people choice. Understood? Hope so.. – Jure Mali Aug 6 '12 at 12:22
  • I can't see anything in the code you've posted that queries custom fields - therefore I assume it's inside $query, so we need to see how the variable's constructed. – TheDeadMedic Aug 6 '12 at 12:51

I guess a meta_query should help you.

$query = new WP_Query( array(array(
                                    'key' => 'wpcf-resitevtags',
                                    'value' => '1-1-1-1' 
                                    'compare' => '='
  • thx for helping. but this works only, when combination is 1-1-1-1. I added this only for example. What if answer is 2-1-1-2 and query gives me also 2-2-1-1-2 ? I want only exact result. And there are more results than 1-1-1-1. – Jure Mali Aug 6 '12 at 13:18
  • Yes indeed, I too had put it for demonstration purpose! You have to find a way to pass those custom field values to the query – Rutwick Gangurde Aug 6 '12 at 13:40
  • hm.. I don't get it. This code works, but it shows me too much. For example. If I would search word "cat", it would show me also pages that have word like "categories", "caterpillar", "mercator",... But I want only pages with words cat in it. – Jure Mali Aug 6 '12 at 14:26
  • Was I misunderstood? I'm stucked and I would appreciate any Ideas. – Jure Mali Aug 6 '12 at 16:49

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.