I have made lot of research and have tested lot of recommended codes, but still can't find the right way to make it work. Pagination links not working. It is navigating me to the 404 page (nothing found on /?s=ddd page). In Some cases it is working, but can't understand why not for all cases ( WORKS ) here is the code I am using. The strange thing is , this code working great on other website ( LINK ), but not working in the current on ( link ) . Permalinks was setted to ' /%category%/%postname%/ ', but I have tested with other types. Still no result.
global $wpdb;
global $wp_query;
// If you use a custom search form
// $keyword = sanitize_text_field( $_POST['keyword'] );
// If you use default WordPress search form
$keyword = get_search_query();
$keyword_meta = '%'.like_escape( $keyword ) . '%';
$keyword_title = like_escape( $keyword ) . '%';
$post_title_ids = $wpdb->get_col( $wpdb->prepare( "SELECT {$wpdb->posts}.ID FROM {$wpdb->posts}
WHERE {$wpdb->posts}.post_title LIKE '%s'", $keyword_title ) );
// Search in all custom fields
$post_ids_meta = $wpdb->get_col( $wpdb->prepare( "SELECT {$wpdb->posts}.ID FROM {$wpdb->postmeta}
LEFT JOIN {$wpdb->posts} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id
WHERE (meta_key = '_key' AND meta_value LIKE '%s') GROUP BY {$wpdb->posts}.ID", $keyword_meta ) );
$paged =(get_query_var('paged')) ? get_query_var('paged') : 1;
$post_ids = array_merge($post_title_ids,$post_ids_meta);
// Query arguments
$args = array(
'post_status' => 'publish',
'paged' => $paged,
'orderby'=>'post__in',
'post__in' => $post_ids
);
$query = new WP_Query ( $args );
//set_include_order($query, $post_ids);
$temp = $wp_query;
$wp_query = null;
$wp_query = $query;
if ($query->have_posts() ):
while ($query->have_posts() ) : $query->the_post(); ?>
.
.
HTML
.
.
<?php endwhile;
endif;?>
<div class="paged_links">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'posts_per_page' => 10,
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages,
'mid_size'=>2,
'next_text'=>_("»"),
'prev_text'=>_("«"),
) );
$wp_query = null;
$wp_query = $temp;
?>
</div>