Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My posts have a bunch of custom fields, to help insert and display custom info easily. I am trying to build a search that would select and display posts according values of specific 2 custom fields. The first field "type" is a radio button in my wp-admin, so in database its meta value is always one word. But second field is checkboxes, so in database meta value has several words, and I need to get posts that have the one visitor selects.

What I have so far:

  1. Search form, that consists of 2 dropdowns, each for one custom field. I use it one homepage: pastebin.com/zxPuGduW
  2. A function in functions.php, to select posts according the values set in dropdowns: pastebin.com/Z6D0GM4q
  3. A results page to show the posts according selections in the dropdowns form: pastebin.com/KHK0exWn
share|improve this question
1  
What's your question exactly? Why not just use WP_Query instead of a raw SQL query? –  Milo Aug 24 '13 at 16:54
1  
Please edit relevant code into the question itself. A question should not be dependent upon a third party site. –  s_ha_dum Aug 24 '13 at 16:55
    
Oh, sorry, the question is that it does not work... And I cannot figure it out why. I tried WP_Query way, but was not able to make it search across multiple custom fields at once. –  universal_wannabe Aug 24 '13 at 16:58
    
I was not able to post code here formatted nicely, and I didn't know it is not allowed to use another site...sorry. –  universal_wannabe Aug 24 '13 at 17:00
    
"it does not work" is not a question. Where/how does it fail? You need to do some of the debugging yourself. This site has perfectly adequate code formating-- look for the {} button-- though it would be nice if it had line numbers. –  s_ha_dum Aug 24 '13 at 17:20

1 Answer 1

You should be able to do this with WP_Query's meta_query parameters:

$type = 'something';
$programme = 'another';

$args = array(
    'meta_query' => array(
        array(
            'key' => 'type',
            'value' => $type,
            'compare' => '='
        ),
        array(
            'key' => 'programme',
            'value' => $programme,
            'compare' => 'LIKE'
        )
    )
);
$query = new WP_Query( $args );
share|improve this answer
    
OMG, totally! Thank you so much. I am trying that right now. Will update on the results. –  universal_wannabe Aug 24 '13 at 18:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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