Support » Plugins » Custom Fields Search

  • Resolved jdhcreative


    Is it possible to write a search that asks for various different Meta_Keys?

    I have posts with multiple custom field entries.

    Lets say a user wants to search for posts where I was reading a specific book (entered in the custom field) and only display the ones where I was happy when posting (another custom field).

    More Background:

    I figured out how to modify the search form. I entered all the inputs just to test. Then when you set the form action to home and request the variables you just set, you can call them (did a few echos to test. said “you serached for $var1 $var2, etc)

    Got it hooked up, but it doesn’t work when I go trying to define the same thing over and over.

    Any suggestions on an approach here?

      $candy = $_REQUEST['candy'] ;
      $mood = $_REQUEST['mood'] ;
      $booktype = $_REQUEST['booktype'] ;
      $bookpgs = $_REQUEST['bookpgs'] ; 
     $querystr = "
        SELECT wposts.*
        FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
        WHERE wposts.ID = wpostmeta.post_id
    	AND wpostmeta.meta_key = 'candy'
    	AND wpostmeta.meta_value = '$candy'
    	AND wpostmeta.meta_key = 'mood'
    	AND wpostmeta.meta_value = '$mood'
    	AND wpostmeta.meta_key = 'booktype'
    	AND wpostmeta.meta_value = '$booktype'
    	AND wpostmeta.meta_key = 'bookpgs'
    	AND wpostmeta.meta_value = '$bookpgs'
        AND wposts.post_status = 'publish'
        AND wposts.post_type = 'post'
        ORDER BY wposts.post_date DESC
     $pageposts = $wpdb->get_results($querystr, OBJECT); ?>

    Thanks 🙂

Viewing 15 replies - 16 through 30 (of 32 total)
  • WPChina


    @stephencronin: thanks for the code!! I have been waiting and looking for this too for a loooong time.

    But how should we implement the code in your last reply? Is that for generating a search form or for generating the output?


    Dude, that is awesome.

    Yea, it’s been 9 long grueling months since I first posted this (and I had been working on it long before I asked for help) I finally just picked up a book, learned php/mysql, and made up my own damn solution outside of WP.

    @the world

    What you see here is a solution. I haven’t put my real idea up to apply this method to LOL(candy and bookpages?) but yea, if this is something you’re looking to do read this post carefully, PRINT TO PDF! And there’s more answers then one. This is powerful. A php book wouldn’t hurt either.

    *Cracks a victory beer* (and one for Stephen as well)

    @kafkaesqui — if you happen to read this, sorry again for the prior rudeness and thanks again for your past help. Reading back, that was just me in a dry, losing money on projects frenzy. Code and empty pizza boxes everywhere. Hope you know how it goes!



    I am still confused, deeply…

    Ok, I tried to duplicate this as much as possible: I created fields for candy, mood, booktype, bookpgs.

    And then I took the code above from StephenCronin’s last message wit his code, and when I add that to my index.php, the code disappears — I see the normal page without anything different (yes, I already cleared the cache).

    Am I using this wrong? I want to create a search for the four fields of candy, mood, booktype, bookpgs. Does this not do that? Am In interpreting this thread totally incorrect? 🙁

    tks for your help.

    Yes please, we need more info : where must we place de code ? in functions.php, in search.php ? 🙂



    I tried adding it to my index.php and search.php, but nothing appears — I see the rest of the page, but the code does not appear and when I View Source, it also does not show. This means that at least I know the PHP is written well and it’s not causing problems 🙂 but it also means that something is not being called to display.

    The computer I wrote all my original code and did my WP ‘hacking’ on was stolen a month ago, otherwise I’d post the code right up. The ‘lifelong’ stuff of course I had backed up elsewhere however I lost a big chunk of my work across the board. :-/

    I still plan on finishing the problem, and I will post my solutions (and example of different implementations) on my personal playground with code for all.


    (and I’ll post it here too! lol I just thought of that walking away… would be nice huh – duh. But it will be around Jan 1st before I get to it, so I don’t want to just leave the thread dead. )



    @jdhcreative: Thanks!! 🙂

    I will be sure to bump this in a few weeks to get your attention and I have already bookmarked your site and will look forward to Jan 1, 2009 (or thereabouts).



    It’s January 1… any word on your progress? 😉

    this is rad!
    Just what I have been looking for..

    @ jdhcreative
    Thanks for the work so far.. looking forward to seeing more info on how to implement it.

    I support all your cause 😉

    I need to know how to use this code! Please help!

    Im ready to give a donation as sure.

    why you guys just dont use google custom search like i do…?



    Google Custom Search has a few drawbacks and it’s not as tightly integrated into WordPress as a plugin or script might be. most importantly, we want to search specific fields and some of those fields might be stored in the database and not obvious to Google… 😉

    This exact problem was brought to my attention last month by a client of mine who does web design. He had a client that needed the WordPress search to include custom fields in the search page.

    I’ve posted the full details on my blog:
    WordPress Custom Field Search Plugin

    I wanted to make sure to post my solution here since many of the posts did help me out as I worked on my solution.

Viewing 15 replies - 16 through 30 (of 32 total)
  • The topic ‘Custom Fields Search’ is closed to new replies.