Created
September 29, 2021 08:54
-
-
Save Alimir/7d0cdde59658cebd52a8779b128b08ee to your computer and use it in GitHub Desktop.
Get top posts based on like_amount meta field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function wp_ulike_pro_custom_top_posts($atts){ | |
$args = array( | |
'post_type' => 'post', | |
'orderby' => array( 'meta_value_num' => 'DESC') , | |
'meta_query' => array( | |
'relation' => 'OR', | |
array( | |
'key' => 'like_amount', | |
'compare' => '>=', | |
'value' => 1, | |
'type' => 'NUMERIC' | |
), | |
array( | |
'key' => 'like_amount', | |
'compare' => 'NOT EXISTS' | |
) | |
) | |
); | |
// The Query | |
$the_query = new WP_Query( $args ); | |
ob_start(); | |
// The Loop | |
if ( $the_query->have_posts() ) { | |
echo '<ul>'; | |
while ( $the_query->have_posts() ) { | |
$the_query->the_post(); | |
echo '<li>' . get_the_title() . sprintf( ' (%d)', do_shortcode( '[wp_ulike_counter]' ) ) . '</li>'; | |
} | |
echo '</ul>'; | |
} else { | |
// no posts found | |
} | |
/* Restore original Post Data */ | |
wp_reset_postdata(); | |
return ob_get_clean(); | |
} | |
add_shortcode( 'wp_ulike_pro_custom_top_posts', 'wp_ulike_pro_custom_top_posts' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment