Support » Fixing WordPress » search not working after db restore

  • Resolved scottiescotsman

    (@scottiescotsman)



    <?php
    header(" echo home_url(); /". $_GET['title'] );
    $mo = $_GET['title'];
    ?>
    $args = array(
    'post_type'             => 'movies',
    'orderby'               => 'title',
    'order'                 => ASC,
    'posts_per_page'        => -1,
    'ignore_sticky_posts'   => TRUE,
    'substring_where'       => $mo
    );

    The above code worked great until I restored my database.
    I can search with a value of 0 and view all the posts but not with the substring_where equals anything else apart from 0.

Viewing 14 replies - 1 through 14 (of 14 total)
  • Moderator bcworkz

    (@bcworkz)

    Hi Scottie. What happens with $args? Does it go into something like $query = new WP_Query( $args );? There is no valid argument called “substring_where” in the default WP_Query. You must have a theme or plugin that has added it as a custom query var. That module may have also made related DB entries that got corrupted when you restored your DB.

    Try to identify what module is adding “substring_where”. The ‘nix command grep or similar can help you identify the module, it’s likely the only module with that string in PHP code besides template files. Try asking the authors of that module how to proceed. Use whatever dedicated support channel they may offer.

    In the interim, try changing ‘substring_where’ to just ‘s’. This invokes the default search routine. It will not search in the same way, but you should get something returned.

    thanks for the help.
    changing the substring_where to just s searches the entire title of the $mo = $_GET[‘title’];.

    function restrict_by_first_letter( $where, $qry ) {
        global $wpdb;
        $sub = $qry->get('substring_where');
        if (!empty($sub)) {
            $where .= $wpdb->prepare(
            " AND SUBSTRING( {$wpdb->posts}.post_title, 1, 1 ) = CONVERT('%s',char) ", $sub
            );
        }
        return $where;
    }
    
    add_filter( 'posts_where' , 'restrict_by_first_letter', 1 , 2 );

    think this might need tweaked…

    hope this helps

    Moderator bcworkz

    (@bcworkz)

    Ah! So that’s how ‘substring_where’ gets thrown into the mix.

    Do you have code white listing ‘substring_where’ as a query var? It would be added to an array passed in a “query_vars” filter callback. You won’t be able to get a valid value in your “posts_where” callback unless this is done.

    Try outputting $sub in your “posts_where” callback to ensure the value made it this far. It can be hard to spot this output on the page unless you output newlines before and after and view the page’s HTML source. Alternately, output using error_log() if you have ready access to your error logs.

    On the template, output the global $wp_query->request, which is the complete SQL used for the query. Any cause for a query failure should be evident by examining this query string.

    SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND (wp_posts.ID = '86') AND wp_posts.post_type = 'page' ORDER BY wp_posts.post_date DESC

    hope this helps

    ok I think I got it sorted apart from sort by title…hmm

    <?php 
        global $wpdb;
    $results = $wpdb->get_results(
    	"
    	SELECT * FROM $wpdb->posts
    	WHERE post_title LIKE '$mo%'
    	AND post_type = 'movies'
    	AND post_status = 'publish'; 
    	"
    );
    
    if ( $results ) {
        foreach ( $results as $post ) {
        setup_postdata ( $post ); 
    ?> 

    every time I try to add sort by = ‘title’ I get no results lol weird …. getting close lol

    Moderator bcworkz

    (@bcworkz)

    You need to add global $post; or make the initial global statement global $wpdb, $post;. Then the subsequent use of the_title() or whatever template tags will work correctly.

    <?php 
        global $wpdb, $posts;
    $results = $wpdb->get_results(
    	"
    	SELECT * FROM $wpdb->posts
    	WHERE post_title LIKE '$mo%'
    	AND post_type = 'movies'
    	AND post_status = 'publish'; 
    	"
    );
    sort($results);
    if ( $results ) {
        foreach ( $results as $post ) {
        setup_postdata ( $post ); 
    ?>

    didn’t work grrrrrrrrr

    Moderator bcworkz

    (@bcworkz)

    You want $post, not $posts on line 2 🙂

    oops sorry

    scottiescotsman

    (@scottiescotsman)

    <?php 
    global $wpdb, $post;
    $results = $wpdb->get_results(
    "
    SELECT * FROM $wpdb->posts
    WHERE post_title LIKE '$mo%'
    AND post_type = 'movies'
    AND post_status = 'publish'
    ORDER BY post_title ASC;
    "	
     );
    sort( $results );
    if ( $results ) {
    foreach ( $results as $post ) {
    setup_postdata ( $post ); 
    ?>

    Doesn’t work either grr

    Moderator bcworkz

    (@bcworkz)

    Doesn’t work how? No results, wrong results, or wrong order?

    I don’t think sort( $results ); is going to work the way you want. You shouldn’t need it with ORDER BY in the query.

    Is $mo assigned the correct value?

    scottiescotsman

    (@scottiescotsman)

    yes $mo is assigned correctly but just wont list alphabetically lol so simple but wont do it lol

    Oh wrong order

    scottiescotsman

    (@scottiescotsman)

    would you believe it when I remover sort( $results ) it worked perfectly lol

    Moderator bcworkz

    (@bcworkz)

    😀 Awesome! FWIW, sort() doesn’t work in this situation because it’s being fed an array of post objects. sort() cannot know what object property sort by. It’s not that sophisticated of a function. If for some reason you did need to sort objects with PHP, you would need to use usort() where you define your own custom sorting logic. This will never be as efficient as SQL ORDER BY clauses.

Viewing 14 replies - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.