0

I have a search page where if the main search returns 0 results then the following query will be used.

The code is

$num_res = $wp_query->post_count;
$get_search_term = get_search_query();

if($num_res == 0)
{

    $args = array(
        'post_type'     => 'resources',
        'meta_key'      => 'resource_txt',
        'meta_value'    => $get_search_term,
        'meta_compare'  => 'LIKE' );
    $custom_query =  new WP_Query( $args );
}

Now I am getting an error which says "Not unique table/alias: 'wp_postmeta'"

When I print the second query in browser it says,

SELECT SQL_CALC_FOUND_ROWS 

DISTINCT wp_posts.ID FROM wp_posts 

INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) 

LEFT JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) 

WHERE 1=1 

AND ( ( wp_postmeta.meta_key = 'resource_txt' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '%insurance claim%' ) ) 

AND wp_posts.post_type = 'resources' 

AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') 

GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10

Is it like, I have to close the previous query and then run the custom query. Any help is highly appreciated.

2 Answers 2

0

Exactly right about resetting it! You should use wp_reset_query(). Try this:

$num_res = $wp_query->post_count;
$get_search_term = get_search_query();

// No more work required for the query, so...
wp_reset_query();

if($num_res == 0)
{
    $args = array(
        'post_type'     => 'resources',
        'meta_key'      => 'resource_txt',
        'meta_value'    => $get_search_term,
        'meta_compare'  => 'LIKE' );
    $custom_query =  new WP_Query( $args );

    // Do stuff here.

    wp_reset_query();
}

It is a good habit to reset the query once you've retrieved what you need from it.

1
  • Thanks for your suggestion. I tried with resetting but it didn't work. I don't know why wordpress is using inner join and left join together without making alias.
    – Raj
    Commented Aug 2, 2016 at 15:03
0

Try using 'pre_get_posts' hook instead of writing another query. Please go through the docs for more information.

 <?php
function search_posts( $query ) {

    // Make sure this only runs on the main query on the front page
    if ($query->is_main_query() ) {

        // Exclude posts that have been explicitly set to hidden
        $query->set('meta_query', array(
            'relation' => 'AND',
            // Include posts where the meta key isn't set
            array(
                'key'     => 'resource_txt',
                'value'   => $get_search_term, // A value must exist due to https://core.trac.wordpress.org/ticket/23268
                'compare' => 'LIKE',
            ),
        ) );

    }

}
add_action( 'pre_get_posts', 'search_posts' );
?>

Hope this helps!

4
  • Thanks for the link. The page did not mention how to build up the query using meta key with a meta value.
    – Raj
    Commented Aug 2, 2016 at 15:08
  • I have edited my answer, please have a look!! Commented Aug 2, 2016 at 15:22
  • Tried with it but same error I am getting. I don't know why the wordpress is adding " INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) " whereas already left join is there.
    – Raj
    Commented Aug 2, 2016 at 15:31
  • Try disabling all plugins! and add the below lines to your wp-config.php file: define('WP_DEBUG', true) ini_set('log_errors',TRUE); ini_set('error_reporting', E_ALL); ini_set('error_log', dirname(FILE) . '/error_log.txt'); Check the exact error! Commented Aug 2, 2016 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.