I am using the below query to show 4 most recent sticky posts in Wordpress.
<?php
$sticky = get_option( 'sticky_posts' ); // Get all sticky posts
rsort( $sticky ); // Sort the stickies, latest first
$sticky = array_slice( $sticky, 0, 4 ); // Number of stickies to show
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) ); // The query
if (have_posts() ) { while ( have_posts() ) : the_post(); ?>
ALL OF MY OUTPUTTED CODE GOES HERE - EDITED OUT TO SAVE SPACE
<?php endwhile;?>
<?php } else { echo ""; }?>
<?php wp_reset_query(); ?>
This works great but if I have a scheduled sticky post (to appear at a future date), the query ignores it as one of the sticky posts and only shows 3 - not the 4 it should?
How can I modify below code to make sure no scheduled sticky posts show AND I still retain 4 slots for sticky posts?
UPDATED CODE BELOW SHOWS ALL POSTS - NOT JUST THE MOST RECENT 4 STICKY ONES.
<?php
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 4,
'post__in' => $sticky,
'paged' => 1,
'ignore_sticky_posts' => 1
);
if (have_posts() ) { while ( have_posts() ) : the_post(); ?>
ALL OF MY OUTPUTTED CODE GOES HERE - EDITED OUT TO SAVE SPACE
<?php endwhile;?>
<?php } else { echo ""; }?>
<?php wp_reset_query(); ?>
Limit the number of posts you return in the query, not by slicing the array.
From http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 4,
'post__in' => $sticky,
'ignore_sticky_posts' => 1
);
Take a look at the Codex reference above for examples of loops. Here's the gist of what you need.
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Notice how the new WP_Query accepts the args from above. In the code you posted you weren't doing anything with them.
Related
Looking to output a Advanced Custom Fields repeater in random order. I have a repeater field "profiles" containing 5 "profile" post objects.
My code today is:
// Randomize and shuffle the rows
$rows = get_sub_field('profiles');
shuffle($rows);
$rand_repeater_fields = array_rand( $rows , 4 ); ?>
<?php if( have_rows('profiles') ):
$stage_index = 0; ?>
<?php while ( have_rows('profiles') ) : the_row();
// print rows only if in array
if (in_array(get_row_index() - 1, $rand_repeater_fields)) { ?>
<?php $post_object = get_sub_field('profile'); //row w. post object start
if( $post_object ):
$post = $post_object;
setup_postdata( $post ); ?>
<?php the_permalink();?>
<?php wp_reset_postdata(); ?>
<?php endif; ?> //row w. post object end
<?php // increment index
$stage_index++;
} ?>
<?php endwhile; ?>
This code successfully output 4 of 5 rows in random order, which means that it almost works as I wish.
How do I update this snippet to output ALL fields of the repeater fields, in random order, even if they are 3 or 10 in total?
Thankful for any suggestions!
<?php
$args = array(
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'rand',
'meta_query' => array(
array(
'key' => 'profiles',
'compare' => 'EXISTS'
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$profiles = get_field('profiles');
if( $profiles ):
foreach( $profiles as $profile ):
echo '<div class="profile">';
echo '<h2>' . $profile['profile'] . '</h2>';
echo '</div>';
endforeach;
endif;
}
}
wp_reset_postdata();
?>
I have a posts loop in home.php:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 2
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_title();
the_content();
}
wp_reset_postdata();
} ?>
How can I add pagination (numbers)? I looked for a tutorial. I tried a lot of functions but none worked.
Thank You
Should be possible using this code:
<?php the_posts_pagination( array( 'mid_size' => 2 ) ); ?>
mid_size defines how many page numbers will be displayed on either side of the current page in that line.
For more details including individual texts for the previous/next page links see:
https://codex.wordpress.org/Function_Reference/the_posts_pagination
I'm trying to exclude posts by the author with the ID "1" form WP_Query. This still shows all posts by all users.
Any thoughts?
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query( array( 'author' => -1 ) );
$wp_query->query('posts_per_page=35'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<div class="grid__third">
...
</div>
<?php endwhile; // end of loop
?>
Thanks a lot!
try this
global $post;
$args=array('author' => '-1', //excludes users with id 1.
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 12,
'ignore_sticky_posts'=> 1,);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
or
// The Query
$args=array('author' => '-1', //excludes users with id 1.
'post_type' => 'post','post_status' => 'publish','posts_per_page' => 12,'ignore_sticky_posts'=> 1,);
$the_query = new WP_Query( $args ); // The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();}else {
// no posts found
}
If your query is not working with
$wp_query = new WP_Query( array( 'author' => -1 ) );
then, You can try some other logic. Like inside the loop get the ID of the author by using POST ID:
$post_author_id = get_post_field( 'post_author', $post_id ); // You will get your author id here
Then apply condition like if($post_author_id != 1){ // YOUR POST } This will display post of all author except author with ID = 1.
Full code like:
while ($wp_query->have_posts()) : $wp_query->the_post();
$post_author_id = get_post_field( 'post_author', HERE YOUR POST ID );
if($post_author_id != 1){
?>
<div class="grid__third">
...
</div>
<?php
} endwhile; // end of loop
Hope this will helpful for you. Thanks.
I have a wordpress website and in the custom template for the category 10 I have this code:
<?php
function new_excerpt_length($length) {return 30;} add_filter('excerpt_length', 'new_excerpt_length');
global $post;
$args = array( 'numberposts' => 5, 'category' => 10 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
echo get_the_post_thumbnail($post_id, $size)?></div><div id="eu_post_category">
<h2><a class="roll-link" href="<?php the_permalink(); ?>"><span data-title="<?php the_title(); ?>"><?php the_title(); ?></span></a></h2>
<h6><?php the_excerpt(); ?></h6>
<?php endforeach; ?>
This returns the last five posts in my category. But I do not want to lose the older posts. I want to have two buttons newer posts||older posts at the bottom in order to let the user see all the posts.
What I've tried: setting the max number in settings-> reading and to use this code in functions.php (with no results):
function limit_posts_per_archive_page() {
if ( is_category('10') )
set_query_var('posts_per_archive_page', 5); // or use variable key: posts_per_page
}
add_filter('pre_get_posts', 'limit_posts_per_archive_page');
Thanks!
< when u click on new posts/older posts button get the page number(which is stored inside $paged varible) pass this inside the arguments
<?php
$args = array(
'paged' => $paged,
'category' => 10,
'posts_per_page' => 5,
'post_status' => 'publish');
query_posts($args);
if ( have_posts() ) {
while ( have_posts() ) { the_post();
echo $post->post_title;
}
}
?>
for references:
http://codex.wordpress.org/Function_Reference/query_posts http://codex.wordpress.org/Template_Tags/get_posts
I have a specific category-slug.php page.
I want to display the first category "banner-ads" with only 1 post per page and then below that, display 3 posts per page from the category "featured."
I don't want to use:
**query_posts( 'posts_per_page=4' );**
I've tried the pre_get_posts function but can't seem to get it working.
Right now the number of posts per page that's displaying is the number I assigned in Settings->Reading
Here's my current code:
$args1 = array(
'category_name' => 'banner-ads',
'posts_per_page' => 1
);
$the_query = new WP_Query( $args1 );
while ( $the_query->have_posts() ) :
$the_query->the_post();
ar2_render_posts( null, array ( 'type' => 'node' ), true );
endwhile;
wp_reset_postdata();
$args2 = array(
'category_name' => 'featured',
'posts_per_page' => 3
);
$query2 = new WP_Query( $args2 );
while( $query2->have_posts() ):
$query2->next_post();
ar2_render_posts( null, array ( 'type' => 'traditional' ), true );
endwhile;
wp_reset_postdata();
You haven't made reset: wp_reset_postdata();
Useful about categories: http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
Here is mine for one of pages
First loop:
<?php
$postq1 = new WP_Query(
array(
'post_type' => array('post','yourcustom'),
'posts_per_page' => 1,
'category_name'=>'banner-ads')
);
if($postq1->have_posts()):
while ( $postq1->have_posts() ) :
$postq1->the_post();?>
<article id="post-<?php the_ID();?>">....</article>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
Second loop:
<?php
$postq2 = new WP_Query(
array(
'post_type' => array('post','yourcustom'),
'posts_per_page' => 3,
'category_name'=>'featured')
);
if($postq2->have_posts()):
while ( $postq2->have_posts() ) :
$postq2->the_post();?>
<article id="post-<?php the_ID();?>">....</article>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
... rest of code with query_posts();
Okay so I finally figured it out. If you want to force it to take certain number of posts then add the filter with its function then at the end of the loop remove it. I realized for my second loop that the ar2_render_posts() function was conflicting with the code so I'm opting to redoing that function from scratch since its basically a layout function.
add_filter('post_limits', 'my_post_limits');
function my_post_limits( $limit ) {
if ( in_category('banner-ads') ) {
return 'LIMIT 0, 3';
}
return $limit;
}
$args1 = array(
'category_name' => 'banner-ads'
);
$the_query = new WP_Query( $args1 );
while ( $the_query->have_posts() ) :
$the_query->the_post();
ar2_render_posts( null, array ( 'type' => 'node' ), true );
endwhile;
wp_reset_postdata();
remove_filter('post_limits', 'my_post_limits');
it is pretty simple, just alter query_args in ar2_render_posts
ar2_render_posts( null, array (
'query_args' => array ( 'posts_per_page' => 1 ),
), true );