0

I spent hours trying to find a solution to this, but I am not able to figure it out. I have a table that shows user's data and I am trying to add pagination to it.

I have tried by adding "paged" in the array but when I click on the link I get "Page not found".

If I understand correctly the problem is created due to the fact that I have a table generated instead of "calling" posts.

Here's what I have now:

    <?php if ( !function_exists('gb_is_user_merchant_role') || !gb_is_user_merchant_role( array( 'deal_admin' ) ) ): ?>
<div class="dashboard_container section main_block">
<?php   
// Purchase history
    if ( gb_account_merchant_id() ) {
        $deals = null;
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $args = array(
            'post_type' => gb_get_deal_post_type(),
            'post__in' => gb_get_merchants_deal_ids(gb_account_merchant_id()),
            'post_status' => 'publish',
            'gb_bypass_filter' => TRUE,
            'posts_per_page' => 5, // return this many
            'paged' => $paged,
            'tax_query' => array(
                array(
                    'taxonomy' => gb_get_deal_cat_slug(),
                    'field'    => 'id',
                    'terms'    => array( 81,82,83,84,85,86,87,88,89,90,91,92 ),
                    'operator' => 'NOT IN',
                ),
            ),

        );
        if ( isset( $_GET['filter'] ) && $_GET['filter'] != '-1' ) {
            $args['tax_query'][] = array(
                'taxonomy' => gb_get_deal_cat_slug(),
                'field' => 'id',
                'terms' => array( $_GET['filter'] ),                    
                );
        }
        $deals = new WP_Query($args);
        if ($deals->have_posts()) {
            ?>

        <?php if ( !function_exists('gb_is_user_merchant_role') || !gb_is_user_merchant_role( array( 'coupon_admin' ) ) ): ?>
            <span class="specialLink_here" style="font-size:16px;"><a href="<?php gb_merchant_purchases_report_url( gb_account_merchant_id() ) ?>" class="report_button"><?php gb_e('Purchase History') ?></a>  |  </span>
            <?php if ( function_exists( 'gb_sales_summary_report_url' ) ): ?>
                <span class="specialLink_here" style="font-size:16px;"><a href="<?php gb_sales_summary_report_url() ?>" class="report_button"><?php gb_e('Sales Summary Report') ?></a>  |  </span>                 
            <?php endif ?>
        <?php endif ?>

        <?php if ( !function_exists('gb_is_user_merchant_role') || gb_is_user_merchant_role( array( 'merchant_admin', 'sales_admin' ) ) ): ?>
            <?php if ( function_exists( 'sec_get_users_report_url' ) ): ?>
                <span class="specialLink_here" style="font-size:16px;"><a href="<?php echo sec_get_users_report_url() ?>" class="report_button"><?php gb_e('Customer Report') ?></a></span>                 
            <?php endif ?>
        <?php endif ?>

        <table class="report_table merchant_dashboard" style="margin-top:20px;"><!-- Begin .purchase-table -->

        <thead>
        <tr>
        <th class="contrast th_status" style="padding:10px;"><?php gb_e('Status'); ?>     </th>
        <th class="purchase-purchase_deal_title-title contrast" style="padding:10px;"><?php gb_e('Deal'); ?></th>
        <th class="contrast th_total_sold" style="padding:10px;"><?php gb_e('Total Sold'); ?></th>
        <th class="contrast th_published" style="padding:10px;"><?php gb_e('Published'); ?></th>
        <th class="contrast th_category" style="padding:10px;">
            <form action="" method="get" >
                <?php
                    $selected = ( isset( $_GET['filter'] ) && $_GET['filter'] != '' ) ? $_GET['filter'] : 0 ;
                    $args = array(
                    'show_option_none'   => gb__('Category Filter'),
                    'orderby'            => 'name',
                    'hide_empty'         => 1,
                    'exclude'            => '81,82,83,84,85,86,87,88,89,90,91,92', // comma separated list of ids.
                    'echo'               => 0,
                    'name'               => 'filter',
                    'selected'           => $selected,
                    'taxonomy'           => gb_get_deal_cat_slug() );
                    $select = wp_dropdown_categories( $args );
                    $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
                    echo $select;
                    ?>
                <noscript><div><input type="submit" value="View" /></div></noscript>
            </form>
        </th>
        <th class="contrast th_reports" style="padding:10px;"><?php gb_e('Reports'); ?></th>
        </tr>
        </thead>

        <tbody>

        <?php
        while ($deals->have_posts()) : $deals->the_post();

            // Build an array of the deal's categories.             
            $category_array = array();
            $cats = gb_get_deal_categories( get_the_ID() );
            foreach ( $cats as $cat ) {
                $category_array[] = '<a href="'.get_term_link( $cat->slug, gb_get_deal_cat_slug() ).'">'.$cat->name.'</a>';
            } ?>
            <tr id="published_deal_<?php the_ID() ?>">

            <td class="td_status">

                <?php if ( !function_exists('gb_is_user_merchant_role') || !gb_is_user_merchant_role( array( 'sales_admin' ) ) || ( gb_is_user_merchant_role( array( 'sales_admin' ) ) && in_array( gb_get_status(), array( 'open', 'closed' ) ) ) ): ?>
                    <span class="alt_button<?php if (gb_get_status() == 'closed') echo ' contrast_button' ?>"><a href="<?php the_permalink() ?>"><?php echo gb_get_status() ?></a></span>
                    <br/>

                    <?php if ( !function_exists('gb_is_user_merchant_role') || !gb_is_user_merchant_role( array( 'coupon_admin', 'sales_admin' ) ) ): ?>
                        <a href="#" class="deal_suspend_button alt_button contrast_button" rel="<?php the_ID() ?>"><?php gb_e('Suspend') ?></a>
                    <?php endif ?>  
                <?php endif ?>  
            </td>

            <td class="purchase_deal_title">
            <?php the_title() ?>
            <br/>
            <a href="<?php the_permalink() ?>" target="_blank"><?php gb_e('View Deal') ?></a>
            <?php if ( !function_exists('gb_is_user_merchant_role') || !gb_is_user_merchant_role( array( 'coupon_admin', 'sales_admin' ) ) ): ?>
                <a href="<?php gb_deal_edit_url() ?>" target="_blank"><?php gb_e('Edit') ?></a>
            <?php endif ?>
            </td> 

            <td class="td_total_sold"><?php gb_number_of_purchases() ?></td>

            <td class="td_published"><p><?php printf( gb__('Published: %s'), get_the_date() ) ?></p><p><?php printf( gb__('Modified: %s'), get_the_modified_date() ) ?></p></td>

            <td class="td_category"><?php echo implode( ', ', $category_array ) ?></td>

            <td class="td_reports">
            <?php if ( !function_exists('gb_is_user_merchant_role') || !gb_is_user_merchant_role( array( 'coupon_admin' ) ) ): ?>
                <span class="report_button"><?php gb_merchant_purchase_report_link() ?></span>
            <?php endif ?>
            <span class="report_button"><?php gb_merchant_voucher_report_link() ?></span>
            </td>

            </tr>
            <?php
        endwhile; ?>
        </tbody>
        </table><!-- End .purchase-table -->

        <?php
        } else {
            echo '<p>'.gb__('No sales info.').'</p>';
        }
    } else {
        echo '<p>'.gb__('Restricted to Businesses.').'</p>';
    }
?>

<?php if (  $deals->max_num_pages > 1 ) : ?>
                    <div id="nav-below" class="navigation clearfix">
                        <div class="nav-previous"><?php next_posts_link( gb__( '<span class="meta-nav">&larr;</span> Older deals' ), $deals->max_num_pages ); ?></div>
                        <div class="nav-next"><?php previous_posts_link( gb__( 'Newer deals <span class="meta-nav">&rarr;</span>' ), $deals->max_num_pages ); ?></div>
                    </div><!-- #nav-below -->
                <?php endif; ?>
                <?php wp_reset_query(); ?>  
  • gb_bypass_filter is not a vaild argument in WP_Query and you should use wp_reset_postdata(), not wp_reset_query(). Also, previous_posts_links don't have the second parameter of $max_pages. Apart from that, everyrhing looks (on face value) to be correct. The only issue I can think of straight away is that this is a static front page, in which case you should use page and not paged in get_query_var() – Pieter Goosen Feb 28 '15 at 5:34
  • @PieterGoosen Quick Question: What do you mean by previous_posts_links don't have the second parameter of $max_pages? – kat_indo Feb 28 '15 at 6:55
  • See here :-) – Pieter Goosen Feb 28 '15 at 7:01
  • @PieterGoosen Yes, I also checked it in the meantime. I've made the adjustments you proposed, but it's not working still. – kat_indo Feb 28 '15 at 7:09
1

As already pointed out in comments

  • gb_bypass_filter is not a valid parameter for WP_Query. If you want to suppress to effect of filters on your query, add 'suppress_filters' => true to your query arguments

  • previous_posts_link() does not accept two arguments, only one. Unlike next_posts_link(), it does not have the second $max_pages parameter. So you can remove that part from your function

  • When using WP_Query, you should use wp_reset_postdata(), not wp_reset_query(). The latter is used with query_posts which you should never ever use.

  • If this is a static frontpage, you should use page as value to your paged parameter, not paged

I had a second look at your code, and it does seem that your code is a bit disjointed. Make the following adjustments

  • Move your pagination to just below the line endwhile or </table> , depending where you would want to display your pagination. The latter however looks like the correct place

  • Move wp_reset_postdata() to just below your pagination, this should all be between endwhile and the first occurance of } else {. The reason for this is, when there is no posts, what are you resetting :-)

Apart from that, your code should work and paginate as normal. If it does not, try the following

  • Add the suppress_filters argument to your query. This will be a test to see if you don't have external filters that are modifying your query

  • Turn debug on, and check for any obvious bugs and errors

  • Flush your permalinks again by visiting the permalink settings page

  • Dump your custom query (var_dump($deals);) and check thst all inputs and outputs is what you expect them to be. Pay attention to max_num_pages and make sure that you have more than one page

  • Deactivate all plugins one by one to eliminate them as possible causes of your issue. Also, clear all caches. Also try your code on a bundled theme

Apart from that, it is really difficult to say what is causing your issue

  • Thank you very much for the very elaborate instructions. Unfortunately, none of the above fixes the problem. I see the link for previous posts but when I click on it I get a "Page not found" error. – kat_indo Mar 3 '15 at 19:38
  • Unfortunately there is nothing else I can think of. It is impossible to say what is causing if none of the above worked. Best will be, make a back up of your site and db, and reinstall Wordpress – Pieter Goosen Mar 4 '15 at 7:15
-1

The previous_posts_link and next_posts_link functions are for archive page pagination, they both check if is_single is not true, so will not work on any sort of single post, page, or custom post type.

You need to use paginate_links instead: http://codex.wordpress.org/Function_Reference/paginate_links

  • This is just a rewrite from an answer from @Milo to a previous question. Don't copy and reuse answers from other questions without credit. Thank you – Pieter Goosen Feb 28 '15 at 5:39
  • @Milo he's right you know! I learned something from you earlier today and hours later had the opportunity to put it in to practice and help out someone else, thanks for that. – Jason Murray Feb 28 '15 at 5:42
  • @JasonMurray Thank you for the input, but this is not working. Also, your instructions are incomplete as previous_posts_link and `next_posts_link' won't be the only thing that one has to change based on the Codex instructions. – kat_indo Feb 28 '15 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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