• Resolved senyoraangelica

    (@senyoraangelica)


    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.

    http://wordpress.org/plugins/pods/

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Scott Kingsley Clark

    (@sc0ttkclark)

    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

    Thread Starter senyoraangelica

    (@senyoraangelica)

    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.

    Plugin Author Scott Kingsley Clark

    (@sc0ttkclark)

    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.

    Thread Starter senyoraangelica

    (@senyoraangelica)

    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.

    Thread Starter senyoraangelica

    (@senyoraangelica)

    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.
    🙁

    Plugin Author Scott Kingsley Clark

    (@sc0ttkclark)

    Can you replace your /wp-content/plugins/pods/classes/PodsData.php with the contents of this file:

    https://gist.github.com/sc0ttkclark/0aba9bc19b3a166bbbf8/raw/09221a5541cb3f32d3719dbca94765fd5d6e9a21/PodsData.php

    Then test your code again, let me know if it’s resolved.

    Thread Starter senyoraangelica

    (@senyoraangelica)

    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.

    Plugin Author Scott Kingsley Clark

    (@sc0ttkclark)

    Thread Starter senyoraangelica

    (@senyoraangelica)

    Same thing. Only the search form appears. Doesn’t display any items and searching doesn’t work.

    Thread Starter senyoraangelica

    (@senyoraangelica)

    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!

    Plugin Author Scott Kingsley Clark

    (@sc0ttkclark)

    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();
    
    ?>
    Thread Starter senyoraangelica

    (@senyoraangelica)

    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!!!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘how to pass meta_query into find()’ is closed to new replies.