Skip to main content
added 182 characters in body
Source Link

I found that Asad Manzoors answer worked for me. If anyone needs it, my version required paged be implented:

$search_query = trim(esc_html( get_search_query() ));
$posts_per_page = $wp_query->query_vars['posts_per_page'];
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$q1 = new WP_Query(array(
    's' => $search_query,
    'post_type' => array('page', 'post'),
    'posts_per_page' => -1,
    'fields' => 'ids'
));
$q2 = new WP_Query(array(
    'fields' => 'ids',
    'post_type' => array('page', 'post'),
    'posts_per_page' => -1,
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'custom_body',
            'value' => $search_query,
            'compare' => 'LIKE'
        )
    )
));

$unique = array_unique(array_merge($q1->posts, $q2->posts));

// If no posts found, ensure that the $query doesn't select every post.
if (!$unique) {
    $unique = array(-1);
}

$query = new WP_Query(array(
    'post_type' => array('page', 'post'),
    'post__in' => $unique,
    'paged' => $paged,
    'post_status' => 'publish',
    'posts_per_page' => $posts_per_page
)); 

I found that Asad Manzoors answer worked for me. If anyone needs it, my version required paged be implented:

$q1 = new WP_Query(array(
    's' => $search_query,
    'post_type' => array('page', 'post'),
    'posts_per_page' => -1,
    'fields' => 'ids'
));
$q2 = new WP_Query(array(
    'fields' => 'ids',
    'post_type' => array('page', 'post'),
    'posts_per_page' => -1,
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'custom_body',
            'value' => $search_query,
            'compare' => 'LIKE'
        )
    )
));

$unique = array_unique(array_merge($q1->posts, $q2->posts));

// If no posts found, ensure that the $query doesn't select every post.
if (!$unique) {
    $unique = array(-1);
}

$query = new WP_Query(array(
    'post_type' => array('page', 'post'),
    'post__in' => $unique,
    'paged' => $paged,
    'post_status' => 'publish',
    'posts_per_page' => $posts_per_page
)); 

I found that Asad Manzoors answer worked for me. If anyone needs it, my version required paged be implented:

$search_query = trim(esc_html( get_search_query() ));
$posts_per_page = $wp_query->query_vars['posts_per_page'];
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$q1 = new WP_Query(array(
    's' => $search_query,
    'post_type' => array('page', 'post'),
    'posts_per_page' => -1,
    'fields' => 'ids'
));
$q2 = new WP_Query(array(
    'fields' => 'ids',
    'post_type' => array('page', 'post'),
    'posts_per_page' => -1,
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'custom_body',
            'value' => $search_query,
            'compare' => 'LIKE'
        )
    )
));

$unique = array_unique(array_merge($q1->posts, $q2->posts));

// If no posts found, ensure that the $query doesn't select every post.
if (!$unique) {
    $unique = array(-1);
}

$query = new WP_Query(array(
    'post_type' => array('page', 'post'),
    'post__in' => $unique,
    'paged' => $paged,
    'post_status' => 'publish',
    'posts_per_page' => $posts_per_page
)); 
Source Link

I found that Asad Manzoors answer worked for me. If anyone needs it, my version required paged be implented:

$q1 = new WP_Query(array(
    's' => $search_query,
    'post_type' => array('page', 'post'),
    'posts_per_page' => -1,
    'fields' => 'ids'
));
$q2 = new WP_Query(array(
    'fields' => 'ids',
    'post_type' => array('page', 'post'),
    'posts_per_page' => -1,
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'custom_body',
            'value' => $search_query,
            'compare' => 'LIKE'
        )
    )
));

$unique = array_unique(array_merge($q1->posts, $q2->posts));

// If no posts found, ensure that the $query doesn't select every post.
if (!$unique) {
    $unique = array(-1);
}

$query = new WP_Query(array(
    'post_type' => array('page', 'post'),
    'post__in' => $unique,
    'paged' => $paged,
    'post_status' => 'publish',
    'posts_per_page' => $posts_per_page
));