Advertisement
Guest User

Untitled

a guest
Oct 28th, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.83 KB | None | 0 0
  1.     function psi_get_playlists_by_user( $user_id, $excluded_playlists_game_id = 0 ) {
  2.  
  3.         error_log( "psi_get_playlists_by_user() called" );
  4.  
  5.         if ( false !== $user_id && is_numeric( $user_id ) && 0 < $user_id ) {
  6.             $output = get_transient( 'user_playlists_' . $user_id );
  7.             if ( false === $output ) {
  8.                 $args = array(
  9.                     'post_type'         => PSiCorePostTypes::PLAYLISTS,
  10.                     'post_status'       => 'publish',
  11.                     'author'            => $user_id,
  12.                     'order_by'          => 'title',
  13.                     'order'             => 'ASC',
  14.                     'suppress_filters ' => false,
  15.                 );
  16.  
  17.                 # Get the user playlist IDs that already have this game saved in
  18.                 if ( is_numeric( $excluded_playlists_game_id ) && 0 < $excluded_playlists_game_id ) {
  19.                     $excluded_playlists_args = array(
  20.                         'meta_query' => array(
  21.                             array(
  22.                                 'key'     => 'psi_playlist_games_$_item',
  23.                                 'compare' => '!=',
  24.                                 'value'   => $excluded_playlists_game_id,
  25.                             ),
  26.                         ),
  27.                     );
  28.                     $args                    = array_merge( $args, $excluded_playlists_args );
  29.                 }
  30.  
  31.                 # Get playlists
  32.                 $user_playlists = new WP_Query( $args );
  33.  
  34.                 error_log( 'exdluded game ID: ' . $excluded_playlists_game_id );
  35.                 error_log( 'Found posts: ' . $user_playlists->found_posts );
  36.  
  37.                 if ( $user_playlists ) {
  38.                     $output = $user_playlists;
  39.                 } else {
  40.                     $output = [];
  41.                 }
  42.                 set_transient( 'user_playlists_' . $user_id, $output, 5 );
  43.                 //set_transient( 'user_playlists_' . $user_id, $output, DAY_IN_SECONDS );
  44.             }
  45.         } else {
  46.             return false;
  47.         }
  48.  
  49.         return $output;
  50.     }
  51.  
  52.     add_filter( 'posts_where', 'get_user_playlists_by_user_posts_where' );
  53.     function get_user_playlists_by_user_posts_where( $where ) {
  54.         $where = str_replace( "meta_key = 'psi_playlist_games_$", "meta_key LIKE 'psi_playlist_games_%", $where );
  55.  
  56.         return $where;
  57.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement