Skip to content

Instantly share code, notes, and snippets.

@Alimir
Last active June 5, 2022 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alimir/a94b329ae90d4bba71483a663f80bf03 to your computer and use it in GitHub Desktop.
Save Alimir/a94b329ae90d4bba71483a663f80bf03 to your computer and use it in GitHub Desktop.
A sample shortcode to display current user pinned posts
<?php
function wp_ulike_pro_get_current_user_pinned_posts($atts){
global $post;
$attributes = shortcode_atts( array(
'post_type' => 'post',
'past_days' => '',
'per_page' => 10
), $atts );
if( strpos( $attributes['post_type'], ',' ) ){
$attributes['post_type'] = explode(',', $attributes['post_type']);
}
$currentUser = is_user_logged_in() ? get_current_user_id() : wp_ulike_generate_user_id( wp_ulike_get_user_ip() );
$getPosts = NULL;
if( empty( $attributes['past_days'] ) ){
$pinnedItems = wp_ulike_get_meta_data( $currentUser, 'user', 'post_status', true );
// Exclude like status
$pinnedItems = ! empty( $pinnedItems ) ? array_filter($pinnedItems, function($v, $k) {
return $v == 'like';
}, ARRAY_FILTER_USE_BOTH) : NULL;
if( ! empty( $pinnedItems ) ){
$getPosts = get_posts( array(
'post_type' => $attributes['post_type'],
'post_status' => array( 'publish', 'inherit' ),
'posts_per_page' => $attributes['per_page'],
'post__in' => array_reverse( array_keys( $pinnedItems ) ),
'orderby' => 'post__in'
) );
}
} else {
$getPosts = wp_ulike_get_most_liked_posts( $attributes['per_page'], $attributes['post_type'], 'post', array(
'start' => wp_ulike_pro_get_past_time( $attributes['past_days'] ),
'end' => current_time( 'mysql' )
), array( 'like' ), false, 1, $currentUser );
}
$output = '';
if( ! empty( $getPosts ) ){
ob_start();
foreach ( $getPosts as $post ) :
setup_postdata( $post ); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
endforeach;
wp_reset_postdata();
$output = sprintf( '<ul>%s</ul>', ob_get_clean() );
}
return $output;
}
add_shortcode( 'user_pinned_posts', 'wp_ulike_pro_get_current_user_pinned_posts' );
@Alimir
Copy link
Author

Alimir commented Dec 20, 2021

If you want to use this code inside our profile builder (pro version), you can replace the following variable:
$currentUser = is_user_logged_in() ? get_current_user_id() : wp_ulike_generate_user_id( wp_ulike_get_user_ip() );
with this code:
global $wp_ulike_user_profile_id; $currentUser = $wp_ulike_user_profile_id;

  • Then you can use this [user_pinned_posts] shortcode inside your profile builder tabs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment