WordPress.org

Ready to get started?Download WordPress

Ideas

WP_QUERY able to search by post_title, post_content plus meta_value and taxonomy

  1. teledirigido
    Member

    When we want to search for something we normally use the field "s => $search" for "post_title" and "post_content". This works fine. When we want to filter by "meta_query" we can do it as well as "tax_query", This works sweet. But we actually can't search by "meta_query", "post_type" and "post_content" at the same time unless we add this snippet

    // Search in all custom fields
        $post_ids_meta = $wpdb->get_col( $wpdb->prepare( "
            SELECT DISTINCT post_id FROM {$wpdb->postmeta}
            WHERE meta_value LIKE '%s'
        ", $search ) );
    
        // Search in post_title and post_content
        $post_ids_post = $wpdb->get_col( $wpdb->prepare( "
            SELECT DISTINCT ID FROM {$wpdb->posts}
            WHERE post_title LIKE '%s'
            OR post_content LIKE '%s'
        ", $search, $search ) );
    
        $post_ids = array_merge( $post_ids_meta, $post_ids_post );
    
        $args = array(
            'post_type'   => 'new',
            'post_status' => 'publish',
            'post__in'    => $post_ids,
        );

    Snippet from: http://www.deluxeblogtips.com/2012/04/search-all-custom-fields.html

    The issue is that when we want to find a string on s=> $search parameter or meta_key, The WP_Query search with and AND rather than an OR. So, I recon it would be really good if we can select at the WP_QUERY the ability to find using OR meta_query and also by tax_query :)

    Cheers!

    Posted: 10 months ago #

RSS feed for this topic

Reply

You must log in to post.

  • Rating

    12345
    0 Votes
  • Status

    This idea is under consideration