how to pass meta_query into find()
-
Hello! I tried using meta_query but I’m having difficulties. It’s been days and I’m confused. I’ve been trying and trying but I couldn’t make it work. =(
This is the recent code that I did.
<form method="get" class="" action=""> <input name="type" value="books" type="hidden"> <input class="" name="search" value="" type="text"> <input class="" value="Search" type="submit"> </form> <?php $params = array( 'orderby' => 'book_year DESC', 'limit' => 2, 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'book_author', 'value' => '$s', ), ) ); $pod_books = pods( 'books' ); echo $pod_books->find( $params )->template( 'book_detail' ); echo $pod_books->pagination(); ?>
I’m going crazy. =(
All I want to do is be able to search my pod’s custom fields with my search form and have the results displayed in a pods template.
Please please please please please please please please help me… I’ve tried a lot of things that I’ve found through googling and reading the forums but so far I can’t make it work.
-
Please don’t post your threads across our Pods site forums, the wordpress.org forums, Stackexchange, and on GitHub. It only needs to be in one place, I recommend wordpress.org forums because it’s more visible to people that aren’t just Pods users. Also, I monitor them all, so since I’ll probably be the one responding, it’s just going to bug me 🙂
Documentation on find() can be found here: http://pods.io/docs/code/pods/find/
Pods find() uses ‘where’, not ‘meta_query’ like WP_Query uses. It does accept the ‘meta_query’ syntax though, so you can just change the array key from ‘meta_query’ to ‘where’.
Also, you’re using
'$s'
but that’s not going to do anything. Did you define $s in PHP? You’re probably wanting to use'value' => pods_var( 'search' )
instead, it should cover what you’re after.Either way, like I said in your other thread asking about searching Pods custom fields, it’s not yet supported in the default search. Since it’s not supported, your ‘where’ usage will be in addition to anything searched, so only items that have those keywords in the title will be pulling up. To get around this, try setting
'search' => false
and putting all of the fields you want searched manually into the ‘where’ parameter. Also consider using'relation' => 'OR'
instead too, otherwise it has to match all of the fields you put in, not just any.http://wordpress.org/support/topic/how-to-make-pods-search-form-search-custom-fields?replies=3
Hi! I sincerely thank you for the response and I am extremely grateful for your patience.
I have edited my code based on your advice but unfortunately, the search form still doesn’t work. Now, the form returns every item I have regardless of what words I search for. It also returns an empty pod template at the end of the results. Here is the code.
<form method="get" class="" action=""> <input name="type" value="books" type="hidden"> <input class="" name="search" value="" type="text"> <input class="" value="Search" type="submit"> </form> <?php $pod_books = pods( 'books' ); $params = array( 'orderby' => 'book_year DESC', 'limit' => 2, 'search' => false, 'where' => array( 'relation' => 'OR', array( 'key' => 'book_title', 'value' => pods_var( 'search' ), ), array( 'key' => 'book_author', 'value' => pods_var( 'search' ), ), array( 'key' => 'book_year', 'value' => pods_var( 'search' ), ) )); echo $pod_books->find( $params )->template( 'book_detail' ); echo $pod_books->pagination(); ?>
I’m really really sorry to bother you with my problem. And thank you again for the time and effort you’ve spent in putting up with me.
Once you hit search, go to the URL and add &pods_debug_sql=1 and then copy/paste the SQL query that appears directly above the ‘book_detail’ template output.
Hi! Here it is. Thank you again for responding.
SELECT DISTINCT <code>t</code>.* FROM <code>wp_posts</code> AS <code>t</code> LEFT JOIN <code>wp_postmeta</code> AS <code>book_title</code> ON <code>book_title</code>.<code>meta_key</code> = 'book_title' AND <code>book_title</code>.<code>post_id</code> = <code>t</code>.<code>ID</code> LEFT JOIN <code>wp_postmeta</code> AS <code>book_author</code> ON <code>book_author</code>.<code>meta_key</code> = 'book_author' AND <code>book_author</code>.<code>post_id</code> = <code>t</code>.<code>ID</code> LEFT JOIN <code>wp_postmeta</code> AS <code>book_year</code> ON <code>book_year</code>.<code>meta_key</code> = 'book_year' AND <code>book_year</code>.<code>post_id</code> = <code>t</code>.<code>ID</code> WHERE ( ( <code>book_title</code>.<code>meta_value</code> = 'jane' ) OR ( <code>book_author</code>.<code>meta_value</code> = 'jane' ) OR ( <code>book_year</code>.<code>meta_value</code> = 'jane' ) OR ( <code>t</code>.<code>post_type</code> = "books" ) ) ORDER BY <code>book_year</code>.<code>meta_value</code> DESC, <code>t</code>.<code>menu_order</code>, <code>t</code>.<code>post_title</code>, <code>t</code>.<code>post_date</code> LIMIT 0, 2
SELECT DISTINCT *, <code>t</code>.<code>ID</code> AS <code>pod_item_id</code> FROM <code>wp_posts</code> AS <code>t</code> WHERE ( ( <code>t</code>.<code>ID</code> = 53 ) AND ( <code>t</code>.<code>post_type</code> = "attachment" ) )
In case you want or need to examine the search form, I have emailed you the url. I sincerely thank you again for all the help.
Hello. I’ve been trying really hard but I couldn’t find a solution. Is there a way I can prevent the search from automatically including
OR ( <code>t</code>.<code>post_type</code> = "books" )
in WHERE? (This is why the search is returning every item I have?)
Also, I don’t understand why the code I made above is returning an empty pods template.
🙁Can you replace your /wp-content/plugins/pods/classes/PodsData.php with the contents of this file:
Then test your code again, let me know if it’s resolved.
Hi! I’m really sorry but it didn’t work. Now, only the search form appears. It doesn’t display any items and searching a word doesn’t do anything.
Thanks for testing, can you try this one instead?
Same thing. Only the search form appears. Doesn’t display any items and searching doesn’t work.
Hi Scott!
I get the same bug even when I use ‘relation’ => ‘AND’
Debug shows the extra query at the end:WHERE ( (
book_title.
meta_value= 'zero' ) AND (
book_author.
meta_value= 'zero' ) AND (
book_year.
meta_value= 'zero' ) AND (
t.
post_type= "books" ) )
However, only the search form appears. It doesn’t display any items and searching a word doesn’t do anything.
Unlike using ‘relation’ => ‘OR’ which returns every item I have regardless of what words I search for, and It also returns an empty pod template at the end of the results.
Anyway, thank you for all the help. I’m looking forward and hoping for a fix to this bug soon. Keep up the great work!
I updated GitHub with the fix, but basically the second Gist I posted earlier worked great, there was a few changes to your code that I had to make:
<?php $pod_books = pods( 'books' ); // Search form echo $pod_books->filters(); // << added this for a dynamic search form that includes the search terms in it $params = array( 'orderby' => 'book_year DESC', 'limit' => 2, 'search' => false, 'where' => array( 'relation' => 'OR', array( 'key' => 'book_title', 'value' => pods_var( 'search' ), 'compare' => 'LIKE' // << added this so it searches contents, not exact match ), array( 'key' => 'book_author', 'value' => pods_var( 'search' ), 'compare' => 'LIKE' // << added this so it searches contents, not exact match ), array( 'key' => 'book_year', 'value' => pods_var( 'search' ), 'compare' => 'LIKE' // << added this so it searches contents, not exact match ) ) ); echo $pod_books->find( $params )->template( 'book_detail' ); echo $pod_books->pagination(); ?>
Thank you! Thank you! Thank you! Thank you!
Again, I’m extremely grateful for the time and effort you’ve spent in putting up with me! PODS is the best!!!
- The topic ‘how to pass meta_query into find()’ is closed to new replies.