How to get pagination working with “Pending” Posts - wordpress

I'm working on a site that allows users to browse through pending posts/content on the front-end.
However, I can't seem to get pagination working with those posts. I've got a custom query that brings up the first page of pending posts on a category page, archive, etc.
But the Page 2, 3, etc. doesn't work.
Any thoughts?
Thanks!
Here's the example code I'm working with:
$args = array(
'cat' => $cat_ID,
'paged' => get_query_var('paged'),
'showposts' => 50,
'posts_per_page' => 50,
'post_status' => 'pending',
);
query_posts($args);
if( have_posts() ) : while (have_posts()) : the_post();
//Post code inserted here
<?php endwhile; ?>
<?php endif; ?>

WordPress pagination will 404 if there are not enough results in the main query to run to that page.
I'm sure there is a better way, but the only way I can think of round it is to use your custom loop in an archive/search page that has more posts than you have drafts.
For example, add your custom loop to the search.php template and reach it by passing a search query that will generate lots of results (e.g. 'a'). Using search.php will include pages, posts and custom post types in the results so will yield more results.
Use paginate_links for the pagination, which will continue to pass the queryvar on each page and after that you should be good.
On a slightly separate note, I would suggest using WP_Query instead of query_posts. See here for why.

Related

Wordpress: alphabetice posts using custom category template

I am trying to display posts ordered alphabetically by title, only for a certain category. I have tried to follow the instructions in the Codex but I am confused because my code in the template pages looks quite different from the examples in the Codex.
I have a category named "designers" so I duplicated the category.php and named it category-designers.php. Inside, there is a call to a loop-designers.php
Inside the category-designers, I have tried the Codex pice of code:
$args = array( 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC' );
$glossaryposts = get_posts( $args );
foreach( $glossaryposts as $post ) : setup_postdata($post);
get_template_part('loop', 'designers');
endforeach;
But the output is weird: first it displays a list of posts ordered by date, then it displays the same posts but ordered alphabetically, only that the alphabetically ordered list is repeated as many times as posts are (9 in this case).
I know I must be doing something terribly wrong but can't find examples using the get_template_part, they all use a foreach just like in the Codex.
Thanks for your answers.
Edited: in my loop-designers.php I have basically the same as in the loop.php, but with modifications so for that category no date, tags or other info are shown. I pasted the HTML here http://jsfiddle.net/6qdvF/
With the piece of code you have there you are including a loop in a loop. Using get_posts() with a foreach loop is essentially a WP loop, then you are including get_template_part('loop', 'designers'); which is more than likely another loop.
You can remove get_template_part('loop', 'designers'); from your piece of code and just stick in your tags to get the desired content, (i.e the_content(); the_title; the_excerpt; etc..) or you can create a new loop with wp_query() like the below example.
<?php
$query = new WP_Query(array('post_type' => 'post', 'cat' => 1, 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC'));
while ( $query->have_posts() ) : $query->the_post();
?>
// put your content template tags here
<?php endwhile; wp_reset_postdata(); ?>
This loop will query "Posts" and category 1. You can change 'post_type' => 'post', to query other post types by changing the word post to the post type name. Or change the category number to the desired category ID you wish to query.

Get titles of all custom post types in a page in wordpress

I'm outputting a bunch of custom post types within a page. How do I get all the titles of the posts within this current page?
You should look at WP_Query() for outputting custom post types. The code below gets all of your custom posts of the type 'custom_post_type', puts them in a variable called $loop and iterates through it, outputting the title of each post contained within.
<?php
// Get the 'Profiles' post type
$args = array(
'post_type' => 'custom_post_type',
);
$loop = new WP_Query($args);
while($loop->have_posts()): $loop->the_post();
the_title();
endwhile;
wp_reset_query();
?>
There are other arguments that you can pass in to WP_Query() to make it more suitable for your needs. Documentation can be found here.
Right before 'The Loop', you might as well just use:
<?php query_posts('post_type=your_custom_post_type');?>

WordPress : Issue with wp_query and gd star rating plugin

I have used GD Star Rating plugin with my Wordpress installation, for creating 2 different sets of Ratings (Posts and Videos). (I was not able to find any other plugin that supports this feature).
I have used following arguments in one wp_query to display a list of top rated videos, and it's working fine.
$args=array(
'post_type' => 'youtube_videos',
'post_status' => 'publish',
'posts_per_page' => 10,
'gdsr_sort' => 'rating',
'gdsr_multi' => 3
);
On the same page, i am trying to show a list of top rated posts, with following arguments in wp_query, but it's not working.
$args=array(
'posts_per_page' => 4,
'post_type' => 'post',
'gdsr_sort' => 'rating',
'gdsr_multi' => 0
);
I found that issue is with value passes in 'gdsr_multi' varibale, if i remove this variable from the top rated videos 2nd part of code works fine.
I am not being able to run both together, any suggestions please?
For gdsr_multi variable documentation of GD Star rating plugin says:
For standard rating data, don't use this variable, or set it to zero.
To get multi ratings data this variable must be set to multi set id to use.
You should reset your query before start a new one. wp_reset_query(); will destroys the previous query used on a custom Loop. This function should be called after The Loop to ensure conditional tags work as expected. As example:
<?php
query_posts( 'post_parent=5' );
if ( have_posts() ) :
while ( have_posts() ) : the_post();
?><?php the_title() ?><br /><?php
endwhile;
endif;
wp_reset_query();
//your second query will be here.
?>
For more details, you can check Wordpress official codex: wp reset query

Wordpress get_posts (show all attachments) pagination outside of the loop

On one of my Wordpress pages (which is really an image blog site) I'm using masonry.js with the Wordpress function get_posts to dump all attachments to my blog posts and display them in a grid. This works fine. However, there's obviously a lot of images and I was hoping to use the infinitescroll.js with this. The only problem is that the get_posts function, outside the loop, doesn't retain the pagination and therefore the functionality of infinitescroll.js doesn't work.
Here is the code I am using to dump all the attachments:
<?php
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null );
$attachments = get_posts( $args );
if ($attachments) {
foreach ( $attachments as $post ) {
setup_postdata($post);
the_attachment_link($post->ID, true);
the_excerpt();
}
}
?>
Is there anyway of adding in pagination to the original Wordpress get_posts() attachment dump outside of the loop, or can anyone think of a solution?
I've done something similar using the 'offset' parameter for get posts.
Basically with each new call to get posts, simply increase your offset amount by the amount of new thumbnails you want to display each time. When the number of thumbnails returned is less than your offset amount, you have reached the end of your posts.
Another solution is to use the pagination parameters of the Wp_Query class. See here for what these are.

enabling pagination inside wordpress post

i have a specific single page which display a single post.
the thing is that i want to display below it all the other posts which have the same special meta data and i made that to work as well.
the problem starts when i try to make pagination to the list of the posts below.
the single post url is something like that:
blog.com/somepost
and the pagination link to the second page of posts below looks someting like this
blog.com/somepost/page/2
and wordpress automatically redirects me back to
blog.com/somepost
how can i prevent it from redirecting me back?
btw, i"m using something like that:
i"m doing something like this:
while( have_posts() ): the_post();
//here printing the single post
endwhile;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'meta_key' => '_btree_project_id',
'meta_value' => $post->ID,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 8
);
$temp = $wp_query;
$wp_query = new WP_Query( $args );
while( have_posts() ): the_post();
//looping through the related posts here
endwhile;
The reading I've done about WordPress pagination gives me the impression that it is an imperfect feature. It requires the global var $wp_query which stems from the WP_Query object. WP_Query holds the global $wp_query which is necessary for making even basic pagination work. Custom queryies don't have access to $wp_query, nor do they own a var to control pagination. I assume you are using a custom query to grab that single post, and as this article points out, with custom queryies:
the “fix” is to trick WordPress into
using the global $wp_query variable
when using our own custom loops.
The article gives an example of utiilizing the global var in your custom query so that you have access to the query_vars that make pagination possible.
I expect that your permalink structure and a custom query that I'm guessing you are using might not be working because the global $wp_query var isn't available during your loop to show related posts.
What does your code to get, display, and paginate the related posts look like? Can you post?

Resources