Created
October 11, 2018 10:22
-
-
Save srdjan-jcc/7a771b13a2d82fddaf8c079b3a0b6905 to your computer and use it in GitHub Desktop.
Uncode WPML recommended posts widget
This file contains hidden or 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 | |
if ( ! function_exists( 'uncode_most_recommended_posts' ) ) { | |
function uncode_most_recommended_posts( $numberOf, $before, $after, $show_count, $post_type = "post", $raw = false ) { | |
$args = array( | |
'post_type' => $post_type, | |
'post_status' => 'publish', | |
'posts_per_page' => $numberOf, | |
'orderby' => 'meta_value_num', | |
'order' => 'DESC', | |
'meta_query' => array( | |
'relation' => 'OR', | |
array( | |
'key' => '_recommended', | |
'compare' => 'NOT EXISTS' | |
), | |
array( | |
'key' => '_recommended', | |
'type' => 'numeric', | |
'compare' => 'EXISTS' | |
) | |
), | |
'suppress_filters' => false, | |
); | |
$wp_query = new WP_Query( $args ); | |
$posts = $wp_query->get_posts(); | |
if ( $raw ): | |
return $posts; | |
else: | |
foreach ( $posts as $item ) { | |
$post_title = stripslashes( $item->post_title ); | |
$permalink = get_permalink( $item->ID ); | |
$post_count = $item->meta_value; | |
echo $before . '<a href="' . $permalink . '" title="' . $post_title . '" rel="nofollow">' . $post_title . '</a>'; | |
echo $show_count == '1' ? ' (' . $post_count . ')' : ''; | |
echo $after; | |
} | |
endif; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment