WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Pagination breaking when filtering by Tag (4 posts)

  1. Zoee
    Member
    Posted 1 year ago #

    Hi all, I've created some custom code that filters by tag when a form is submitted. It works fine apart from when I try to go to page 2,3,4 etc.

    My URL appears as /?tag=tagname/page/2 if I switch it manually to /page/2/?tag=tagname it loads page 2 perfectly, I just don't know how to alter the code to accommodate this!

    This is my base code:

    if($_GET['tag'] != "") {
    //search by tag
    $searchtag = $_GET['tag'];
    $args = array( 'post_type' => 'projects',
    'tag' => ''.$searchtag.'',
    'posts_per_page' => 4,
    'orderby'=> date,
    'order' => desc,
    'paged'=>$paged
    );
    } else {
    //dont search - show all
    $args = array( 'post_type' => 'projects',
    'posts_per_page' => 4,
    'orderby'=> date,
    'order' => desc,
    //'s' => $s,
    'paged'=>$paged
    );
    }

    <?php query_posts($args);
    if ( have_posts() ) : ?>

    <div class="project-navigation">
    <span class="project-pages">Go to: </span>
    <span>
    <?php
    global $wp_query;
    $total_pages = $wp_query->max_num_pages;

    if ($total_pages > 1){
    $current_page = max(1, get_query_var('paged'));

    echo paginate_links(array(
    'base' => get_pagenum_link(1) . '%_%',
    'format' => '/page/%#%',
    'show_all' => True,
    'current' => $current_page,
    'total' => $total_pages,
    'prev_text' => '« Prev',
    'next_text' => 'Next »'
    ));
    }
    ?>
    </span>
    </div>

    <?php while ( have_posts() ) : the_post();?>

    and so on.

    Help very much appreciated :)

    Zoe

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Are you using the default permalink structure under wp-admin > Settings > Permalinks?
    http://codex.wordpress.org/Using_Permalinks

    If so, try altering:

    'format' => '/page/%#%',

    to this:

    'format' => '&paged=%#%',
  3. Zoee
    Member
    Posted 1 year ago #

    Thank you! That fixed it :)

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome. I'm glad you got it resolved :-)

Topic Closed

This topic has been closed to new replies.

About this Topic