0
votes

I modified WordPress main loop for blog posts, made it query and loop through different types of posts according to what filter is submitted.

At the beginning of the HTML, there is a form with several buttons as option, it's similar to the SA site menu: Questions Tags Users Badges Unanswered.

When visitor click one of the menu button, a new WP_Query is submitted.

Below the options, is the loop of posts, depends on what filter is submitted.

The php codes is basically a standard wordpress loop with customized functions:

   $args= !empty($_POST['filter']) ? $_POST['filter']:null;
    get_option_nav();
    if( my_loop_have_posts(array('filter'=>$args))):
    while my_loop_posts(): the_my_loop_post();
    get_template_part('contents');
    endwhile;
    endif;

Things are working fine except that only the default filter get the correct pagination links. After submit a new filter, when click the pagination links, it goes to the default filter's pages. How to modify wordpress pagination links to get it work for the other filters?

Here's the code that I use for query args:

function get_args($args){
$defaults= array( 
    'order' => 'DESC', 
    'orderby' => 'modified', 
    'max_num_pages' =>5, 
    'paged' => get_paged (), 
    'post_status' => 'any', 
    'post_type' => array ('post', 'docs','topics'),
    'posts_per_page' => 5,
   )
$args = wp_parse_args ( $args, $defaults );
extract ( $args );
if(!empty($args['filter'])){
   switch ($args['filter']){
      case 'top_voted':
         $args['post_type'] = 'docs';
         $args['meta_key'] = '_vote_total';
         $args['order'] = 'DESC';
         $args['orderby']='meta_value_num';
       break;
      case 'unanswered_questions':
         $args['post_type] = 'topics';
         $args['blabla'] = 'blabla';
      break;
      default:
          blabla;
       break;
}
return $args;
}

and the code for the paged:

function get_paged() {
    global $wp_query;
    if ( get_query_var( 'paged' ) ) {
        $paged = get_query_var( 'paged' );

    } elseif ( ! empty( $wp_query->query['paged'] ) ) {
        $paged = $wp_query->query['paged'];
    }

    if ( ! empty( $paged ) )
        return (int) $paged;

    return 1;
}
1

1 Answers

0
votes

When you make a custom query, you need to explicitly call every parameter of the query. Thus, standard ones disappear, as is the pagination case.

Depending on how you are making this new query you could try:

  • To concatenate at the beginning $query_string, for example $query = new WP_Query( $query_string . 'order=DESC' ) As it is explained in the Codex:

If you want to preserve the original query parameter information that was used to generate the current query, and then add or over-ride some parameters, you can use the $query_string global variable in the call to query_posts().

  • To get the pagination from the default query and use it in your custom query $paged = get_query_var('paged'); $query = new WP_Query( array( 'paged' => $paged ) );

In your case there is something wrong with your get_paged() function. I have tested it and it does not work, but I have not found the bug. Instead, I tried what I usually use, and it works for me:

function get_paged() {
    if ( get_query_var('paged') ) {
        $paged = get_query_var('paged');
                } elseif ( get_query_var('page') ) { $paged = get_query_var('page');
                } else { $paged = 1; }
        return $paged;
    }

Something seems to fail in you else if statement. Let me know if this solves your issue.