I am trying to sort posts as most shared.
And i want to avoid have most shared posts on top for very long time.
I suggest to order posts as week group then sort each group as most shared.
who can help with this?
Sébastien | French WordpressDesigner answers:
what do you use to share your posts ?
A.Aziz comments:
I have a meta_data that have number of post shares over social media.
Sébastien | French WordpressDesigner comments:
Yiu can use this code if you want sort your POSTS as most shared
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_key' => 'your_count',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
// 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();
If you want order by week group it's a bigger job
A.Aziz comments:
thanks. I really need it to be order by week, then order each week as most shared.
Sébastien | French WordpressDesigner comments:
you can sort posts by week in a first time as an array, with the total count of share for each week.
In second time, sort this array by count.
Use this new order to create a loop for each week.
Sébastien | French WordpressDesigner comments:
or maybe could you use a bdd request
A.Aziz comments:
where is that?
Monit Jadhav answers:
How will you share posts? Depends on what kind of meta fields or variables it generates so that sorting can be done as per the parameters.
A.Aziz comments:
I have a meta_data that have number of post shares over social media.
Monit Jadhav comments:
Try this code mate I have also included week navigation
<?php
//create two variables $paged and $week and assign it this weeks value using date('W')
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : date( 'W' );
$week = $paged;
/*
Add the date query and the paged variable to the loop
'paged' => $paged,
'date_query' => array(
array(
'year' => date( 'Y' ),
'week' => date( $week ),
),
),
*/
$args = array(
'post_type' => 'post',
'meta_key' => 'post_share_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'posts_per_page' => -1,
'paged' => $paged,
'date_query' => array(
array(
'year' => date( 'Y' ),
'week' => date( $week ),
),
),
);
// The Query
$share_query = new WP_Query( $args );
// The Loop
if ( $share_query->have_posts() ) {
echo '<ul>';
while ( $share_query->have_posts() ) {
$share_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
Echo "<h1>Sorry No posts were found this week</h1>";
$noposts = true;
}
/* this is the end of posts */
/* Post Week Navigation */ ?>
<div>
<?php
next_posts_link( 'Popular Earlier week',date( 'W' ) );
?>
</div>
<?php if(!$noposts): ?>
<div>
<?php
previous_posts_link( 'Popular Last Week' );
?>
</div>
<?php endif; ?>
<?php
/* End of Post Week Navigation */
/* Query reset */
wp_reset_query(); ?>
Let me know if this helps
Arnav Joy answers:
you can order posts by meta key as
<?php
// args
$args = array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'share_count',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
// query
$wp_query = new WP_Query( $args );
// loop
while( $wp_query->have_posts() )
{
$wp_query->the_post();
// ...
}
?>
you have to change "share_count" meta key ..