Custom post order
-
Hello
I have this custom code in my functions.php I would just like to ask if it wont conflict with your plugin as am consider upgrading to the pro version
/**
* Prioritize posts on homepage by sticky status, author type, membership, and content type.
*/
function comprehensive_post_prioritization( $posts, $query ) {
// Only affect the main homepage query (first page).
if ( ! $query->is_main_query() || ! $query->is_home() || $query->get( 'paged' ) > 1 ) {
return $posts;
}
// Use a transient cache for improved performance.
$transient_key = 'comprehensive_post_prioritization_' . md5( wp_json_encode( wp_list_pluck( $posts, 'ID' ) ) );
$cached_posts = get_transient( $transient_key );
if ( false !== $cached_posts ) {
return $cached_posts;
}
$sticky_ids = get_option( 'sticky_posts', [] );
if ( empty( $sticky_ids ) ) {
$prioritized = prioritize_by_content_type( $posts );
set_transient( $transient_key, $prioritized, 5 * MINUTE_IN_SECONDS );
return $prioritized;
}
// Split posts into sticky and non-sticky.
$sticky_posts = [];
$non_sticky_posts = [];
foreach ( $posts as $post ) {
if ( in_array( $post->ID, $sticky_ids, true ) ) {
$sticky_posts[] = $post;
} else {
$non_sticky_posts[] = $post;
}
}
$priority_user = get_user_by( 'login', 'nigerpress' );
$buckets = [
'nigerpress' => [],
'featured' => [],
'admin_author' => [],
'other' => [],
];
$user_cache = [];
$priority_cache = [];
foreach ( $sticky_posts as $post ) {
$user_id = (int) $post->post_author;
// Cache content type priority.
if ( isset( $priority_cache[ $post->ID ] ) ) {
$content_priority = $priority_cache[ $post->ID ];
} else {
$content_priority = get_content_type_priority( $post->ID );
$priority_cache[ $post->ID ] = $content_priority;
}
// Nigerpress user (highest priority).
if ( $priority_user && $user_id === (int) $priority_user->ID ) {
$buckets['nigerpress'][] = [
'post' => $post,
'content_priority' => $content_priority,
];
continue;
}
// Active membership check.
if ( function_exists( 'wc_memberships_is_user_active_member' )
&& wc_memberships_is_user_active_member( $user_id, 48679 )
) {
$buckets['featured'][] = [
'post' => $post,
'content_priority' => $content_priority,
];
continue;
}
// Cache user data per author.
if ( ! isset( $user_cache[ $user_id ] ) ) {
$user_cache[ $user_id ] = get_userdata( $user_id );
}
$user = $user_cache[ $user_id ];
if ( $user && array_intersect( [ 'administrator', 'author' ], (array) $user->roles ) ) {
$buckets['admin_author'][] = [
'post' => $post,
'content_priority' => $content_priority,
];
} else {
$buckets['other'][] = [
'post' => $post,
'content_priority' => $content_priority,
];
}
}
// Sort buckets by content priority (1 = Featured, 2 = Sponsored, 3 = Regular).
foreach ( $buckets as &$group ) {
$group = sort_by_content_priority( $group );
}
unset( $group );
// Combine sorted stickies.
$ordered_stickies = array_merge(
wp_list_pluck( $buckets['nigerpress'], 'post' ),
wp_list_pluck( $buckets['featured'], 'post' ),
wp_list_pluck( $buckets['admin_author'], 'post' ),
wp_list_pluck( $buckets['other'], 'post' )
);
// Prioritize non-sticky posts.
$prioritized_non_stickies = prioritize_by_content_type( $non_sticky_posts );
// Final ordered result.
$final_posts = array_merge( $ordered_stickies, $prioritized_non_stickies );
// Cache the result for 5 minutes.
set_transient( $transient_key, $final_posts, 5 * MINUTE_IN_SECONDS );
return $final_posts;
}
/**
* Get post content type priority.
*
* @param int $post_id Post ID.
* @return int Priority (1 = Featured, 2 = Sponsored, 3 = Regular).
*/
function get_content_type_priority( $post_id ) {
$content_types = get_the_terms( $post_id, 'content_type' );
if ( empty( $content_types ) || is_wp_error( $content_types ) ) {
return 3;
}
foreach ( $content_types as $type ) {
$slug = sanitize_title( $type->slug );
$name = sanitize_text_field( $type->name );
if ( 'featured-posts' === $slug || 'Featured Posts' === $name ) {
return 1;
}
if ( 'sponsored-posts' === $slug || 'Sponsored Posts' === $name ) {
return 2;
}
}
return 3;
}
/**
* Sort posts by content priority (ascending).
*
* @param array $posts_array Array of ['post' => WP_Post, 'content_priority' => int].
* @return array
*/
function sort_by_content_priority( $posts_array ) {
usort(
$posts_array,
static function ( $a, $b ) {
return $a['content_priority'] <=> $b['content_priority'];
}
);
return $posts_array;
}
/**
* Prioritize posts by content type.
*
* @param WP_Post[] $posts Posts array.
* @return WP_Post[]
*/
function prioritize_by_content_type( $posts ) {
$featured = [];
$sponsored = [];
$regular = [];
foreach ( $posts as $post ) {
$priority = get_content_type_priority( $post->ID );
if ( 1 === $priority ) {
$featured[] = $post;
} elseif ( 2 === $priority ) {
$sponsored[] = $post;
} else {
$regular[] = $post;
}
}
return array_merge( $featured, $sponsored, $regular );
}
add_filter( 'the_posts', 'comprehensive_post_prioritization', 10, 2 );Thanks
The topic ‘Custom post order’ is closed to new replies.