תצוגת כמות צפיות מתוך הדשבורד של וורדפרס

PHOTO EMBED

Thu Jun 16 2022 13:25:37 GMT+0000 (UTC)

Saved by @Shesek

// Add this to Quary in elementor: order_by_posts_views
function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' צפיות';
}
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}
 
// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); 
// Add to a column in WP-Admin
add_filter('manage_posts_columns', 'posts_column_views');
add_action('manage_posts_custom_column', 'posts_custom_column_views',5,2);
function posts_column_views($defaults){
    $defaults['post_views'] = __('צפיות');
    return $defaults;
}
function posts_custom_column_views($column_name, $id){
    if($column_name === 'post_views'){
        echo getPostViews(get_the_ID());
    }
}
add_action('wp_head', function() { if ( is_single() ) { setPostViews(get_the_ID()); } });
add_action( 'elementor/query/order_by_posts_views', function( $query ) {
    $query->set( 'meta_key', 'post_views_count' );
    $query->set( 'orderby', 'meta_value_num' );
    $query->set( 'order', 'DESC' );
} );
content_copyCOPY