admin管理员组

文章数量:1134557

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/zxPuGduW
  2. A function in functions.php, to select posts according the values set in dropdowns: pastebin/Z6D0GM4q
  3. A results page to show the posts according selections in the dropdowns form: pastebin/KHK0exWn

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 asked Aug 24, 2013 at 16:49 universal_wannabeuniversal_wannabe 11 bronze badge 7
  • 1 What's your question exactly? Why not just use WP_Query instead of a raw SQL query? – Milo Commented Aug 24, 2013 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 Commented Aug 24, 2013 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 Commented Aug 24, 2013 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 Commented Aug 24, 2013 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 Commented Aug 24, 2013 at 17:20
 |  Show 2 more comments

1 Answer 1

Reset to default 0

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 );

本文标签: query specific posts according their custom fieldsusing sql SELECT