Wordpress query_posts(...) by page - php

With wordpress I am trying to figure out how to get the current page number and use it with the query_posts function i have tried passing in page=1 but that doesnt seem to work
query_posts( "cat=4&posts_per_page=1" ); // how do i pass in the page number???

query_posts( array( 'cat' => 8, 'posts_per_page' => 25, 'paged' => get_query_var('page') ) );

Related

Pagination not Working on Wordpress. Posts doesn't change

I'm trying to configure the Wordpress pagination in de Index homepage.
I've used the WP_Query with the regard of get_query_var('paged') before, just as the code shows:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query( array(
'cat' => 7,
'posts_per_page' => 2,
'paged' => $paged
) );
?>
Then I used the native Wordpress function previous_posts_link() to go back onte the list of posts.
The problem that happens is that when the page loads, the url of the page changes, but the posts that shows are the same as before. Even to page 3 or 4, it just shows the same list of pages.
Where am I going wrong?

Wordpress posts__not_in not functioning

$posts_not_included = array( get_the_ID() );
$args = array(
'posts_per_page' => 3,
'posts__not_in' => $posts_not_included
);
query_posts($args);
I have this code in a single-post page. The page showcases a post, and then I try to call query_posts to get the other posts. However, the post I'm currently on is shown, despite the ID being shown by get_the_ID(); is correct. Am I doing something wrong? Do I need to use the WP_Query class instead?
Your parameter is wrong change it
$posts_not_included = array( get_the_ID() );
$args = array(
'posts_per_page' => 3,
'post__not_in' => $posts_not_included // right argument is post not posts
);
query_posts($args);

Pagination Not working - wordpress

Pagination on my website not working, everything else is fine, But when i click to go to "/page/2/3/" it shows no page found error.
Website : http://savemoney.16mb.com
Error pages: http://savemoney.16mb.com/page/2/
and currently Front Page set to "Latest Posts" (Not Static Page)
Pagination Code (Frontpage.php):
<?php
// show all coupons and setup pagination
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
query_posts( array( 'post_type' => APP_POST_TYPE, 'post_status' => $post_status, 'ignore_sticky_posts' => 1, 'paged' => $paged ) );
?>
<?php get_template_part('loop', 'coupon'); ?>`
Any advice on how to get this pagination working correctly would be appreciated!
Thanks,
T
have you tried , "add_args" => false ?
here is more info for that https://core.trac.wordpress.org/ticket/30831

WordPress: Custom Query

I have a custom query that uses an array merge so I can list articles and custom post type posts in one list, showing 5 at a time. Pagination below allows the user to see the rest of the results. I keep getting pagination errors, but I've tried virtually every variation of the $paged variable in my query to get pagination to work and it doesn't. I know it's me, and probably a simple syntax thing...but I'm stumped. Any ideas? (Note: this page has multiple, other custom queries above the one in question)
Here's my code:
<?php
$loop1 = array_merge( $wp_query->query,
array( 'post_type' => array('post','podcasts', 'cat' => $cat_ID ),
'paged' => ( get_query_var('page') ? get_query_var('page') : 1 ),
'posts_per_page' => 5 ) );
query_posts( $loop1 );
while (have_posts()) : the_post(); ?>
have you tried?
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),

Wordpress custom post type categories

Hey. I am using a custom post type in wordpress. I register this custom post type like this:
register_post_type("lifestream", array(
'label' => 'Lifestream',
'public' => true,
'hierarchical' => true,
'menu_position' => 5,
'supports' => array('title','editor','author','thumbnail','comments','custom-fields'),
'taxonomies' => array('category','post_tag'),
'query_var' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'caller_get_posts' => 1
));
register_taxonomy_for_object_type('category', 'lifestream');
register_taxonomy_for_object_type('post_tag', 'lifestream');
In the theme (the loop template) I like to combine posts and my custom post type, for that I am using query_posts() with these parameters:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => array('post', 'lifestream'),
'paged' => $paged,
'cat' => $wp_query->get('cat'),
'tag' => $wp_query->get('tag'),
'year' => $wp_query->get('year'),
'monthnum' => $wp_query->get('monthnum'),
'post_status' => 'publish',
'showposts' => 3
);
query_posts($args);
# the loop
while ( have_posts() ) : the_post();
# markup
endwhile;
if($wp_query->max_num_pages > 1):
# next_posts_link / previous_posts_link
endif;
wp_reset_query();
This is working so far. But, I got problems with the category and tags pages. If I call the frontpage everything is fine and I can paginate through the pages getting the correct results.
And, if I call a paged URL, e.g. /category/mycat/page/2 a 404 is thrown. But there definitly should be posts. No matter if there are custom type posts or normale posts in the category. I suppose that my parameters for query_posts() aren´t correct, but don´t know ...
It seems that $wp_query->max_num_pages has the wrong value. But why? Do I register the taxonomies (I like to use categories and tags for my custom post types) correctly?
Do you have any Idea what to do? Thanks a lot!
I have just encountered the exact same problem and couldn't find the solution anywhere! The internets are full of resources about the topic but none provided the correct answer to the issue.
Here's the correct answer for anyone searching. Put the below code in functions.php in your theme's root directory.
function init_category($request) {
$vars = $request->query_vars;
if (is_category() && !is_category('Blog') && !array_key_exists('post_type', $vars)) :
$vars = array_merge(
$vars,
array('post_type' => 'any')
);
$request->query_vars = $vars;
endif;
return $request;
}
add_filter('pre_get_posts', 'init_category');
All credits go to Mike who posted this on Wordpress.com. Cheers!
Make sure you add this:
'paged' => get_query_var('paged')
to your $args and it should accept the paging okay.
More here
In the archive.php try using the following:
query_posts( array(
'post_type' => 'lifestream',
'posts_per_page' => 6,
'orderby' => 'menu_order',
'orderby' => 'date',
'order' => 'ASC',
'paged' => '' . get_query_var('paged')
));
if ( have_posts() ) : while ( have_posts() ) : the_post();
endwhile; else:
endif;
For the next and previous page links I use:
next_posts_link('Older Entries', $wp_query->max_num_pages);
previous_posts_link('Newer Entries', $wp_query->max_num_pages);

Categories