2

i need to use wp_query to excute posts from 2 separated keywords using s=keyword so i have found this answer & it is very very useful Combining queries with different arguments per post type

& i achieved my goal this is my code

<?php add_filter( 'posts_search', '__search_by_title_only', 500, 2 ); ?>
<?php
$first=array(
's'         => 'keyword1',
);
$second=array(
's'         => 'keyword2',
);
$first_query = new WP_Query( $first);
$second_query = new WP_Query( $second);
$result = new WP_Query();
$result->posts = array_merge( $first_query->posts, $second_query->posts );
$result->post_count = count( $result->posts );?>
<div class="post-listing">
<?php remove_filter( 'posts_search', '__search_by_title_only', 500 ); ?>
<?php if ( $result->have_posts() ) : while ( $result->have_posts() ) : $result->the_post();?>
//layout 
<?php endwhile; ?>
<?php if ($result->max_num_pages > 1) tie_pagenavi(); ?>
<?php wp_reset_query(); ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

but my problem is the pagenavi not worked for me the <?php if ($result->max_num_pages > 1) tie_pagenavi(); ?> my theme have a custom pagenavi which authors & categories archive use it

my pagenavi code is

<?php
/*
Plugin >> Name: WP-PageNavi
Plugin URI: http://lesterchan.net/portfolio/programming/php/
Description: Adds a more advanced paging navigation to your WordPress blog.
Version: 2.50
Author: Lester 'GaMerZ' Chan
Author URI: http://lesterchan.net
*/


/*  
Copyright 2009  Lester Chan  (email : lesterchan@gmail.com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


### Function: Page Navigation: Boxed Style Paging
function tie_get_pagenavi($before = '', $after = '') {
global $wpdb, $wp_query;
$pagenavi_options = tie_pagenavi_init(); 

if (!is_single()) {
    $request = $wp_query->request;
    $posts_per_page = intval(get_query_var('posts_per_page'));
    $paged = intval(get_query_var('paged'));
    $numposts = $wp_query->found_posts;
    $max_page = $wp_query->max_num_pages;
    if(empty($paged) || $paged == 0) {
        $paged = 1;
    }
    $pages_to_show = intval($pagenavi_options['num_pages']);
    $larger_page_to_show = intval($pagenavi_options['num_larger_page_numbers']);
    $larger_page_multiple = intval($pagenavi_options['larger_page_numbers_multiple']);
    $pages_to_show_minus_1 = $pages_to_show - 1;
    $half_page_start = floor($pages_to_show_minus_1/2);
    $half_page_end = ceil($pages_to_show_minus_1/2);
    $start_page = $paged - $half_page_start;
    if($start_page <= 0) {
        $start_page = 1;
    }
    $end_page = $paged + $half_page_end;
    if(($end_page - $start_page) != $pages_to_show_minus_1) {
        $end_page = $start_page + $pages_to_show_minus_1;
    }
    if($end_page > $max_page) {
        $start_page = $max_page - $pages_to_show_minus_1;
        $end_page = $max_page;
    }
    if($start_page <= 0) {
        $start_page = 1;
    }
    $larger_per_page = $larger_page_to_show*$larger_page_multiple;
    $larger_start_page_start = (tie_n_round($start_page, 10) + $larger_page_multiple) - $larger_per_page;
    $larger_start_page_end = tie_n_round($start_page, 10) + $larger_page_multiple;
    $larger_end_page_start = tie_n_round($end_page, 10) + $larger_page_multiple;
    $larger_end_page_end = tie_n_round($end_page, 10) + ($larger_per_page);
    if($larger_start_page_end - $larger_page_multiple == $start_page) {
        $larger_start_page_start = $larger_start_page_start - $larger_page_multiple;
        $larger_start_page_end = $larger_start_page_end - $larger_page_multiple;
    }
    if($larger_start_page_start <= 0) {
        $larger_start_page_start = $larger_page_multiple;
    }
    if($larger_start_page_end > $max_page) {
        $larger_start_page_end = $max_page;
    }
    if($larger_end_page_end > $max_page) {
        $larger_end_page_end = $max_page;
    }
    if($max_page > 1 || intval($pagenavi_options['always_show']) == 1) {
        $pages_text = str_replace("%CURRENT_PAGE%", number_format_i18n($paged), $pagenavi_options['pages_text']);
        $pages_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pages_text);
        //echo $before.'<div class="pagenavi">'."\n";

                if(!empty($pages_text)) {
                    echo '<span class="pages">'.$pages_text.'</span>';
                }
                if ($start_page >= 2 && $pages_to_show < $max_page) {
                    $first_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options['first_text']);
                    echo '<a href="'.esc_url(get_pagenum_link()).'" class="first" title="'.$first_page_text.'">'.$first_page_text.'</a>';
                    if(!empty($pagenavi_options['dotleft_text'])) {
                        echo '<span class="extend">'.$pagenavi_options['dotleft_text'].'</span>';
                    }
                }
                if($larger_page_to_show > 0 && $larger_start_page_start > 0 && $larger_start_page_end <= $max_page) {
                    for($i = $larger_start_page_start; $i < $larger_start_page_end; $i+=$larger_page_multiple) {
                        $page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
                        echo '<a href="'.esc_url(get_pagenum_link($i)).'" class="page" title="'.$page_text.'">'.$page_text.'</a>';
                    }
                }
                previous_posts_link($pagenavi_options['prev_text']);
                for($i = $start_page; $i  <= $end_page; $i++) {                     
                    if($i == $paged) {
                        $current_page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['current_text']);
                        echo '<span class="current">'.$current_page_text.'</span>';
                    } else {
                        $page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
                        echo '<a href="'.esc_url(get_pagenum_link($i)).'" class="page" title="'.$page_text.'">'.$page_text.'</a>';
                    }
                }
                next_posts_link($pagenavi_options['next_text'], $max_page);
                if($larger_page_to_show > 0 && $larger_end_page_start < $max_page) {
                    for($i = $larger_end_page_start; $i <= $larger_end_page_end; $i+=$larger_page_multiple) {
                        $page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
                        echo '<a href="'.esc_url(get_pagenum_link($i)).'" class="page" title="'.$page_text.'">'.$page_text.'</a>';
                    }
                }
                if ($end_page < $max_page) {
                    if(!empty($pagenavi_options['dotright_text'])) {
                        echo '<span class="extend">'.$pagenavi_options['dotright_text'].'</span>';
                    }
                    $last_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options['last_text']);
                    echo '<a href="'.esc_url(get_pagenum_link($max_page)).'" class="last" title="'.$last_page_text.'">'.$last_page_text.'</a>';
                }

        //echo '</div>'.$after."\n";
    }
}
}


### Function: Round To The Nearest Value
function tie_n_round($num, $tonearest) {
return floor($num/$tonearest)*$tonearest;
}


### Function: Page Navigation Options
function tie_pagenavi_init() {
$pagenavi_options = array();
$pagenavi_options['pages_text'] = __('page %CURRENT_PAGE% من %TOTAL_PAGES%','tie');
$pagenavi_options['current_text'] = '%PAGE_NUMBER%';
$pagenavi_options['page_text'] = '%PAGE_NUMBER%';
$pagenavi_options['first_text'] = __('&laquo; first','tie');
$pagenavi_options['last_text'] = __('last &raquo;','tie');
$pagenavi_options['next_text'] = __('next');
$pagenavi_options['prev_text'] = __('prev');
$pagenavi_options['dotright_text'] = __('...','tie');
$pagenavi_options['dotleft_text'] = __('...','tie');


$pagenavi_options['num_pages'] = 8;

$pagenavi_options['always_show'] = 0;
$pagenavi_options['num_larger_page_numbers'] = 0;
$pagenavi_options['larger_page_numbers_multiple'] = 10;

return $pagenavi_options;
}

?>

note please : iam using this code in custom template within single page, the function __search_by_title_only is in my functions.php to search only the titles in this query

edit when i use <?php if(function_exists('tie_pagenavi') ) { tie_pagenavi('', '', '', '', 3, false); } ?> it is show the pagination div <div class="pagination"></div> the which available in my categories & authors archives but but in this template the div is empty

any idea!

  • Sounds like you will need to post the code for your customized pagenavi. – s_ha_dum Oct 27 '13 at 15:58
1

The WP_Pagenavi FAQ links to the following article to explain how to use the Pagenavi plugin with a secondary query by passing the wp_pagenavi() function a query parameter.

From the tutorial:

$my_query = new WP_Query();

while ( $my_query->have_posts() ) : $my_query->the_post();
    the_title();
    // more stuff here
endwhile;

wp_pagenavi( array( 'query' => $my_query ) );

wp_reset_postdata();    // avoid errors further down the page
| improve this answer | |
  • but i have pagenavi in my theme so i don't need to install another plugin – adnan Oct 27 '13 at 14:48
  • That's a mistake, in my opinion. Where's the benefit? Themes are for displaying things. Plugins are for functionality. I'd advise you to take pagenavi our of your theme, and use the plugin... which has the added benefit of getting automatic updates from the repository. At the very least update your version of pagenavi. – helgatheviking Oct 27 '13 at 14:52
  • i have a custom theme, custom pagenavi & my pagenavi looks nice – adnan Oct 27 '13 at 14:54
  • Nothing is stopping you from styling the actual WP_Pagenavi plugin. You're trying to reinvent the wheel for no reason. Your version of "pagenavi" isn't doing what you'd like, so why wouldn't you use a version that does? Or if you insist, you are free to deconstruct WP_Pagenavi for yourself. – helgatheviking Oct 27 '13 at 15:41
  • my problem is i don't want to change my category, authors pagenavi if this plugin will do the job without changing category, author pagenavi please let me know – adnan Oct 27 '13 at 15:49

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