Show sticky posts at the top, then display other posts - wordpress

I have this query:
<?php $wp_query = array(
'post__not_in' => array(4269),
'post_type' => 'whatson',
'exclude' => '4269',
'posts_per_page' => 5,
'order' => 'ASC',
'orderby' => 'date',
'post_status' =>array('future','published'));
?>
It's currently showing upcoming posts in order... how can I show the sticky posts at the top, then show the other posts below it?
E.g If two posts have been marked as sticky then they would show at the top, then the other 3 posts will just be the upcoming posts.

I had a similar problem a while back and devised the following solution. This will show you how to output a maximum of five posts, with the sticky ones at the top. You will have to make your own adjustments to the arguments array, but this should point you in the right direction.
It's a matter of determining how many sticky posts were in fact displayed, subtracting that number from 5, then displaying the balance of non-sticky posts.
<?php
function define_loop_args($present_cat, $sticky_toggle = 0 ) {
/*the total number of posts to display*/
$this->maxNum = 5;
$loop_args = array(
'cat' => $present_cat,
);
if ( $sticky_toggle == TRUE ) {
$loop_args['post__in'] = get_option( 'sticky_posts' );
$loop_args['posts_per_page'] = $this->maxNum;
} else {
$loop_args['post__not_in'] = get_option( 'sticky_posts' );
$loop_args['posts_per_page'] = ((int)($this->maxNum) - (int)($this->sticky_count));
}
$this->loop_args = $loop_args;
}
?>
<ul class="no-children">
<?php
/*
* STICKY
*output sticky posts first
*/
$this->define_loop_args( $catID, 1 );
/*
*count the number of sticky posts displayed, in order to calculate how many non-sticky posts to output next
*/
$sticky_count = 0;
// The Query
$the_query = new WP_Query( $this->loop_args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<li><?php the_title(); ?>
<?php
$sticky_count++;
endwhile;
// End The Loop
// Reset Post Data
wp_reset_postdata();
$this->sticky_count = $sticky_count;
/*
* NON-STICKY
*output non-sticky posts next
*/
$this->define_loop_args( $catID );
$the_query = new WP_Query( $this->loop_args );
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<li><?php the_title(); ?>
<?php
endwhile;
// End The Loop
// Reset Post Data
wp_reset_postdata();
?>
</ul>

you can get it ny multiple loop as follows
<?php
$sticky = get_option( 'sticky_posts' );
$args_ordinary = array(
'post__not_in' => array(4269,$sticky),
'post_type' => 'whatson',
'exclude' => '4269',
'posts_per_page' => 3,
'order' => 'ASC',
'orderby' => 'date',
'post_status' =>array('future','published'));
$args_sticky = array(
'posts_per_page' => -1,
'post__in' => $sticky,
'posts_per_page' => 2,
'post_type' => 'whatson'
);
query_posts($args_sticky);
if (have_posts()): ?>
<?php while (have_posts()) : the_post(); ?>
//sticky post
<?php endwhile; ?>
<?php endif; ?>
// Now Ordinary Posts
query_posts($args_ordinary);
if (have_posts()): ?>
<?php while (have_posts()) : the_post(); ?>
//ordinary post
<?php endwhile; ?>
<?php endif; ?>

Related

Remove duplicate post variation in WooCommerce

Need a query to remove duplicate post variation like this situation, in all products:
Just load the product edit page by adding this code into the functions.php file of your active theme, then remove the code. Please change the product ID in the code below.
<?php if (isset( $_GET['post'])) {
$current_list = array();
$args = array(
'posts_per_page' => 500,
'post_type' => 'product_variation',
'orderby' => 'title',
'order' => 'asc',
'offset' => 0,
'ID' => 1631819251, //Parent ID
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) {
while ( $the_query->have_posts() ) :
$the_query->the_post(); ?>
<?php the_title(); ?><br>
<?php $this_variation_title = get_the_title();
if (in_array($this_variation_title, $current_list)) {
echo $this_variation_title.' duplicate deleted.<br>';
wp_delete_post(get_the_ID());
} else {
array_push($current_list, $this_variation_title);
echo 'Variation added to array.<br>';
} ?>
<?php endwhile; ?>
<?php } ?>
<?php wp_reset_postdata();
} ?>

Query (2) ACF post objects during WP_Query in custom taxonomy template

Building a Wordpress page template for a custom taxonomy and related custom post type. Within a new WP_Query I need to grab fields from (2) different ACF post object fields; list-staff and list-rep. Code works as expected up to the wp_reset_postdata(); correct amount of results are returned, data within each post is unique up to the point of the reset. After the reset, all data is the same within each post. Code follows, and I'm certain there's a more elegant solution:
<?php
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'parade-of-homes',
'parade-category' => 'parade-homes',
'posts_per_page' => -1,
'meta_key' => 'entry_number',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$listing = new WP_Query( $args );
if ( $listing->have_posts() ) :
while ( $listing->have_posts() ) : $listing->the_post();
?>
<?php the_field('list_number'); ?>
<?php
$staff = get_field('list_staff');
$rep = get_field('list_rep');
if( $staff ):
// override $post
global $post;
$post = $staff;
setup_postdata( $post );
?>
<?php the_permalink(); ?><?php the_title(); ?>
<?php
endif;
if( $rep ):
// override $post
$post = $rep;
setup_postdata( $rep );
?>
<?php the_field('mkt_co'); ?><?php the_field('mkt_tel'); ?>
<?php
endif;
wp_reset_postdata();
?>
<?php the_field('list_address') ?>
<?php
endwhile;
endif;
wp_reset_query();
?>
Figured this one out. setup_postdata() is a completely wrong direction for this application. What is correct is documented on the ACF page for "displaying data for multiple post objects." As the article states "Using this method, the $post object is never changed so all functions need a second parameter of the post ID in question." Read more about it here; https://www.advancedcustomfields.com/resources/post-object/. My working code follows:
<?php
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'parade-of-homes',
'parade-category' => 'parade-homes',
'posts_per_page' => -1,
'meta_key' => 'entry_number',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$listing = new WP_Query( $args );
if ( $listing->have_posts() ) :
while ( $listing->have_posts() ) : $listing->the_post();
?>
<?php
the_field('list_number');
$post_object = get_field('list_staff');
if( $post_object ):
?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<?php echo get_the_title($post_object->ID); ?>
<?php
endif;
?>
<?php
$post_object = get_field('list_rep');
if( $post_object ):
?>
<p><?php the_field('mkt_co', $post_object->ID); ?></span></p>
<?php the_field('mkt_tel', $post_object->ID); ?>
<?php
endif;
?>
<?php the_field('list_address') ?>
<?php
endwhile;
endif;
wp_reset_query();
?>

Wordpress displaying only children posts

im trying to display only children posts, i don't need pagination. In theory this code should query all posts and in second query get posts that have parent. But in reality it displaying all posts with parent.
<?php
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$args1 = [
'post_type' => 'drama',
'post_parent' => 0, // Only return top level pages
'nopaging' => true, // Alias of posts_per_page => -1, Get all top level pages
'fields' => 'ids' // Only get pages ID's for performance
];
$exclude_parents = get_posts( $args1 );
// Now we can run our query as normal
$args = [
'post_type' => 'drama',
'post__not_in' => $excluse_parents, // Exclude parent pages
'paged' => $paged,
];
$query = new WP_Query( $args );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php $parent_p = wp_get_post_parent_id( $post_ID ); ?>
<?php if ( 0 == 0 ) { ?>
<div class="col-md-12"><span><?php the_title(); ?></span></div>
<?php } ?>
<?php endwhile; wp_reset_postdata(); ?>
<div class="clearfix"></div>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
If you are looking for an algo to get those posts which have no child post then the algo should be
Step 1 : get all post id in an array(post_id_arr).
Step 2: get all post's parent id in another array(parent_id_arr);
Step 3: run a loop for parent_id_arr and remove the parent ids from post_id_arr.
Step 4: Now the post_id_arr will have only child post ids ,

Wordpress - Display only posts in the past breaking pagination

I have an events page displaying an 'events' custom post type. At the top of the page I have 2 upcoming posts in an upcoming event list and then at the bottom I have 3 past events in a past events list. Posts are displayed in each section based on if an 'event date' custom field is in the past or not. The past events uses WP page navi plugin to paginate the posts. 'posts_per_page' is set at one as a test.
There should be 3 pages that have 1 past post displayed on them, respectively. Unfortunately, the pagination shows 5 pages, the first two have no posts displayed on them, as if the posts from the upcoming posts lists are behaving as ghosts. I have removed the upcoming posts list as a test but this makes no difference.
Does anyone know any reason for this? I have no idea. Thanks.
<?php
$temp_post = $post; // Store the Page the Post
// Get the current date
$current_date = date('M d, Y', time());
$current_date = strtotime( $current_date );
$wp_query= null;
$wp_query = new WP_Query();
$args = array( 'post_type' => 'events','posts_per_page' => 1,'paged' => $paged,'meta_key' => 'event_date', 'orderby' => 'meta_value_num', 'order' => 'DESC');
$wp_query->query( $args );
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
// Get the date custom field
$post_date = get_field('event_date');
$post_date = strtotime( $post_date );
// If older than post date, don't show it
if( $current_date > $post_date ): ?>
MY POST CONTENT GOES HERE
<?php endif; // END DATE FILTER ?>
<?php endwhile; //END LOOP ?>
<?php /* Start wp-pagenavi support */ ?>
<?php if(function_exists('wp_pagenavi') ) :
echo '<nav class="pag">'; ?>
<?php wp_pagenavi(); ?>
<?php echo '</nav>'; ?>
<?php else: ?>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php endif; ?><?php /* End wp-pagenavi support */ ?>
<?php endif; ?>
<?php $post = $temp_post; // Reset the Post ?>
It's acting as ghost because you are not showing it. You are showing the navigation in the bottom of page. The query returns 5 page, so navigation would be for 5 page.
But you aren't showing the post, if it doesn't meet this condition
// If older than post date, don't show it
if( $current_date > $post_date ): ?>
That's why those posts are not showing. Remove that condition and you will see that all the posts are showing as it should.
If you want to display pagination this way, you would have to limit the number of returned post from query itself.
Thanks a lot, I managed to work it out with a push in the right direction.
// Get the current date
$current_date = date('Ymd', time());
$wp_query= null;
$wp_query = new WP_Query();
$args = array(
'post_type' => 'events',
'posts_per_page' => 1,
'paged' => $paged,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'event_date',
'meta_query' => array(
array(
'key' => 'event_date',
'value' => $current_date,
'compare' => '<',
'type' => 'CHAR'
)
)
);
$wp_query->query( $args );
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
MY POST CONTENT HERE
<?php //endif; // END DATE FILTER ?>
<?php endwhile; //END LOOP ?>
<?php /* Start wp-pagenavi support */ ?>
<?php if(function_exists('wp_pagenavi') ) :
echo '<nav class="pag">'; ?>
<?php wp_pagenavi(); ?>
<?php echo '</nav>'; ?>
<?php endif; ?><?php /* End wp-pagenavi support */ ?>
<?php endif; ?>

how to display multiple post with different category in wordpress page

I need your help..
i've two categories in wordpress to display post
1. News
2. Projects
now i want to display last 3 post from both category like below..
post1.
post2.
post3.
post1.
post2.
post3.
Thank you guys..
you can try something like below code it will fetch 3 post from each category
<?php
wp_reset_query();
$cats = get_categories('');
foreach ($cats as $cat) :
if($cat->category_parent) continue;
$args = array(
'posts_per_page' => 3,
'category_name' => $cat->slug,);
query_posts($args);
if (have_posts()) :
echo '<h2>Latest Posts in '.$cat->name.' Category</h2>';
?>
<?php while (have_posts()) : the_post(); ?>
<div>
<h2>
<?php the_title(); ?>
</h2>
</div>
<?php
if ( is_category($vidcat) ) { the_content(); }
else { echo strip_tags(get_the_excerpt(), '<a><strong>'); }
?>
<!-- this area is for the display of your posts the way you want it -->
<!-- i.e. title, exerpt(), etc. -->
<?php endwhile; ?>
<?php else : echo '<h2>No Posts for '.$cat->name.' Category</h2>';?>
<?php endif; wp_reset_query; ?>
<?php endforeach; ?>
if you want to fetch exclude any category than pass the argument like this.
$args = array(
'exclude' => '' //pass category id which your don't want to include.
)
$cats = get_categories($args);
Thank you guys for helping me...
I found solution which is working..
Thanks a lot for support..
$args1 = array( 'posts_per_page' => 3, 'offset'=> 0, 'category' => 3,'post_type'=> 'post','post_status'=>'publish','orderby'=> 'post_date','order'=> 'DESC','suppress_filters' => true);?>
<?php
$categoryids = array(add news category id,add projects category id);
$args = array(
'numberposts' => 3,
'category__in' => $categoryids,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'give the name of post types',
'post_status' => 'publish' );
$posts_array = get_posts( $args );
foreach ($posts_array as $posts) {
....................
}
?>

Resources