Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've created a plugin that calls a list of posts and grabs external API data. I am using get_posts to grab the list (because I need to feed the array of links into the API query)

For whatever reason, any attempt at pagination either fails completely or loads the same posts. Since nothing I've done has worked, I've removed all the pagination at this point.

add_action('admin_menu' , 'blgs_dashboard_snap'); 

function blgs_dashboard_snap() {

    add_dashboard_page( 'Social Metrics', 'Social Metrics', 'read', 'blgs-social-metrics-snap', 'blgs_metrics_page');
}

function blgs_metrics_page() {


echo '<div class="wrap">';
echo '<div id="icon-blgs-metrics" class="icon32">';
echo '<br />';
echo '</div>';

echo '<h2>Social Metrics Dashboard</h2>';
echo '<div id="bg_social_page">';

        echo '
                  <ul class="headers">
                    <li class="post_date">Publish Date</th>
                    <li class="post_title">Post Title</th>
                    <li class="social_icon social_total"><img src="'.plugins_url( '/img/twitter.png', __FILE__ ).'" title="Twitter" alt="Twitter"></li>
                    <li class="social_icon social_total"><img src="'.plugins_url( '/img/gplus.png', __FILE__ ).'" title="Google Plus" alt="Google Plus"></li>
                    <li class="social_icon social_total"><img src="'.plugins_url( '/img/stumble.png', __FILE__ ).'" title="StumbleUpon" alt="StumbleUpon"></li>
                    <li class="social_icon social_total"><img src="'.plugins_url( '/img/linkedin.png', __FILE__ ).'" title="LinkedIn" alt="LinkedIn"></li>
                    <li class="social_icon social_total"><img src="'.plugins_url( '/img/delicious.png', __FILE__ ).'" title="Delicious" alt="Delicious"></li>
                    <li class="social_icon social_total"><img src="'.plugins_url( '/img/reddit.png', __FILE__ ).'" title="Reddit" alt="Reddit"></li>
                    <li class="social_icon social_total"><img src="'.plugins_url( '/img/digg.png', __FILE__ ).'" title="Digg" alt="Digg"></li>
                    <li class="social_icon social_total"><img src="'.plugins_url( '/img/facebook.png', __FILE__ ).'" title="Facebook" alt="Facebook"></li>
                    <li class="fb_total fb_comm">Comments</li>
                    <li class="fb_total fb_like">Likes</li>
                    <li class="fb_total fb_share">Shares</li>
                  </ul>

        ';

        global $post;
            $args = array(
            'post_type'         => 'post',
            'post_status'       => 'publish',
            'order'             => 'DESC',
            'orderby'           => 'date',
            'posts_per_page'    => 15,
        );


        $dash_posts = get_posts( $args );
        // build out table

        foreach( $dash_posts as $post ) :   setup_postdata($post);

            $link   = get_permalink();
            $grab   = urlencode($link);

            $request    = new WP_Http;
            $url        = 'http://api.sharedcount.com/?url='.$grab.'';
            $response   = wp_remote_get ( $url );

            if( is_wp_error( $response ) ) {
               echo '<p>There was an error getting the data. Please try again later.</p>';
            } else {
                $output = json_decode( $response['body'] ); 
            }


            $title  = get_the_title();
            $edit   = get_edit_post_link( $post->ID, $title );
            $date   = get_the_date('m/d/y');

            // get my numbers
            $stumble    = $output->StumbleUpon;
            $reddit     = $output->Reddit;
            $delicious  = $output->Delicious;
            $gplus      = $output->GooglePlusOne;
            $twitter    = $output->Twitter;
            $digg       = $output->Diggs;
            $linkedin   = $output->LinkedIn;

            // facebook sub counts
            $fb_total   = $output->Facebook->total_count;
            $fb_cmbox   = $output->Facebook->commentsbox_count;
            $fb_click   = $output->Facebook->click_count;
            $fb_comms   = $output->Facebook->comment_count;
            $fb_like    = $output->Facebook->like_count;
            $fb_share   = $output->Facebook->share_count;

            // set even odd for some fancy

            $altrows = ( ' altrow' != $altrows ) ? ' altrow' : '';

            // now output the data
            echo '

                  <ul class="numbers'.$altrows.'">
                    <li class="post_date">'.$date.'</li>
                    <li class="post_title"><a href="'.$edit.'">'.$title.'</a></li>
                    <li class="social_total">'.$twitter.'</li>
                    <li class="social_total">'.$gplus.'</li>
                    <li class="social_total">'.$stumble.'</li>
                    <li class="social_total">'.$linkedin.'</li>
                    <li class="social_total">'.$delicious.'</li>
                    <li class="social_total">'.$reddit.'</li>
                    <li class="social_total">'.$digg.'</li>
                    <li class="social_total">'.$fb_total.'</li>
                    <li class="fb_total fb_comm">'.$fb_comms.'</li>
                    <li class="fb_total fb_like">'.$fb_like.'</li>
                    <li class="fb_total fb_share">'.$fb_share.'</li>
                  </ul>
                ';

        endforeach;
        wp_reset_postdata();

echo '</div></div>';

}

any ideas about (a) getting it to work and (b) where it should go?

share|improve this question
    
You need to add a paged value into your args array. Hard core a value for testing if you like. –  t31os Jan 2 '12 at 15:05
add comment

1 Answer

up vote 1 down vote accepted

This is what ended up solving it (I'll include the relevant parts)

    $total = wp_count_posts()->publish;
    $perpage = 10;
    $curpage = isset( $_GET['pagenum'] ) ? intval($_GET['pagenum']) : 1;

    global $post;
        $args = array(
    'post_type'         => 'post',
    'post_status'       => 'publish',
    'order'             => 'DESC',
    'orderby'           => 'date',
    'posts_per_page'    => $perpage,
    'offset'            => $perpage*($curpage-1)
    );

$pages = ceil($total/$perpage);
$dash_posts = get_posts( $args );

handles the query, then

    $pagin = array();
for( $i = 1; $i <= $pages; $i++ ) {
    $url = $_SERVER['REQUEST_URI'] . "&pagenum=$i";
    $link = "<li><a href='$url'>$i</a></li>";
    if ($curpage != $i) $link = str_replace( '~', '', $link );
    $pagin[] = $link;
}
echo '<div id="blgs_post_loader"><ul>'. implode( '', $pagin ) .'</ul></div>';

took care of the pagination itself. For whatever reason, the standard "paged" variable doesn't fire correctly on the admin side.

share|improve this answer
1  
Only problem with this is that the URL will keep appending the pagenum to the existing URL so if you're on pagenum1 and go to pagenum 2, the url will be example.com?pagenum=1&pagenum=2 –  AlxVallejo Nov 14 '12 at 17:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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