Last active
April 3, 2023 13:15
-
-
Save Alimir/aba51be63d283f6596d88d31fae7ad9f to your computer and use it in GitHub Desktop.
Make user pinned posts query type for Bricks builder
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 | |
/* Add new query type control to query options */ | |
add_filter( 'bricks/setup/control_options', 'ulp_setup_query_controls'); | |
function ulp_setup_query_controls( $control_options ) { | |
/* Adding a new option in the dropdown */ | |
$control_options['queryTypes']['ulp_pinned_posts'] = esc_html__( 'WP ULike - User pinned posts' ); | |
return $control_options; | |
}; | |
/* Run new query if option selected */ | |
add_filter( 'bricks/query/run', 'ulp_maybe_run_new_query', 10, 2); | |
function ulp_maybe_run_new_query( $results, $query_obj ) { | |
/* If option is selected, run our new query */ | |
if ( $query_obj->object_type === 'ulp_pinned_posts' ) { | |
$results = ulp_run_new_query(); | |
} | |
return $results; | |
}; | |
/* Setup post data for posts */ | |
add_filter( 'bricks/query/loop_object', 'ulp_setup_post_data', 10, 3); | |
function ulp_setup_post_data( $loop_object, $loop_key, $query_obj ) { | |
if ( $query_obj->object_type !== 'ulp_pinned_posts' ) { | |
return $loop_object; | |
} | |
global $post; | |
$post = get_post( $loop_object ); | |
setup_postdata( $post ); | |
return $loop_object; | |
}; | |
/* Return results from our custom WP Query arguments */ | |
function ulp_run_new_query() { | |
$currentUser = is_user_logged_in() ? get_current_user_id() : wp_ulike_generate_user_id( wp_ulike_get_user_ip() ); | |
$getPosts = NULL; | |
$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; | |
$args = [ | |
'post_type' => 'post', | |
'post_status' => array( 'publish' ), | |
'posts_per_page' => 10, | |
'post__in' => ! empty( $pinnedItems ) ? array_reverse( array_keys( $pinnedItems ) ) : [0], | |
'orderby' => 'post__in' | |
]; | |
$posts_query = new WP_Query( $args ); | |
return $posts_query->posts; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment