Skip to content

Instantly share code, notes, and snippets.

@Alimir
Created September 29, 2021 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Alimir/7d0cdde59658cebd52a8779b128b08ee to your computer and use it in GitHub Desktop.
Save Alimir/7d0cdde59658cebd52a8779b128b08ee to your computer and use it in GitHub Desktop.
Get top posts based on like_amount meta field
<?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