Skip to content

Instantly share code, notes, and snippets.

@kylephillips
Last active October 14, 2022 19:33
Show Gist options
  • Select an option

  • Save kylephillips/9fe12f195c671c989af3 to your computer and use it in GitHub Desktop.

Select an option

Save kylephillips/9fe12f195c671c989af3 to your computer and use it in GitHub Desktop.

Revisions

  1. kylephillips revised this gist Sep 22, 2015. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions favorites-display.php
    Original file line number Diff line number Diff line change
    @@ -17,15 +17,22 @@
    */
    $favorites = get_user_favorites();
    if ( $favorites ) : // This is important: if an empty array is passed into the WP_Query parameters, all posts will be returned
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; // If you want to include pagination
    $favorites_query = new WP_Query(array(
    'post_type' => 'post', // If you have multiple post types, pass an array
    'posts_per_page' => -1,
    'ignore_sticky_posts' => true,
    'post__in' => $favorites
    'post__in' => $favorites,
    'paged' => $paged // If you want to include pagination, and have a specific posts_per_page set
    ));
    if ( $favorites_query->have_posts() ) : while ( $favorites_query->have_posts() ) : $favorites_query->the_post();
    // Treat this like any other WP loop
    endwhile; endif; wp_reset_postdata();
    endwhile;

    next_posts_link( 'Older Favorites', $favorites_query->max_num_pages );
    previous_posts_link( 'Newer Favorites' );

    endif; wp_reset_postdata();
    else :
    // No Favorites
    endif;
  2. kylephillips revised this gist Jul 23, 2015. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions favorites-display.php
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,7 @@
    <?php
    // This function will accept an optional User ID parameter
    $favorites = get_user_favorites();

    // Method 1: simple foreach loop
    $favorites = get_user_favorites();
    if ( isset($favorites) && !empty($favorites) ) :
    foreach ( $favorites as $favorite ) :
    // You'll have access to the post ID in this foreach loop, so you can use WP functions like get_the_title($favorite);
    @@ -16,9 +15,10 @@
    * Passing an empty array as an argument returns ALL posts.
    * @see https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
    */
    if ( $favorites ) :
    $favorites = get_user_favorites();
    if ( $favorites ) : // This is important: if an empty array is passed into the WP_Query parameters, all posts will be returned
    $favorites_query = new WP_Query(array(
    'post_type' => 'post',
    'post_type' => 'post', // If you have multiple post types, pass an array
    'posts_per_page' => -1,
    'ignore_sticky_posts' => true,
    'post__in' => $favorites
  3. kylephillips revised this gist Jul 23, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions favorites-display.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    // This function will accept an optional User ID parameter
    $favorites = get_user_favorites();

  4. kylephillips renamed this gist Jul 23, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions favorites-display → favorites-display.php
    Original file line number Diff line number Diff line change
    @@ -19,6 +19,7 @@
    $favorites_query = new WP_Query(array(
    'post_type' => 'post',
    'posts_per_page' => -1,
    'ignore_sticky_posts' => true,
    'post__in' => $favorites
    ));
    if ( $favorites_query->have_posts() ) : while ( $favorites_query->have_posts() ) : $favorites_query->the_post();
  5. kylephillips revised this gist Jul 4, 2015. 1 changed file with 13 additions and 3 deletions.
    16 changes: 13 additions & 3 deletions favorites-display
    Original file line number Diff line number Diff line change
    @@ -8,12 +8,22 @@ if ( isset($favorites) && !empty($favorites) ) :
    endforeach;
    endif;

    // Method 2: WP_Query Object

    /**
    * Method 2: WP_Query Object
    * Check if there are favorites before instantiating the WP_Query.
    * Passing an empty array as an argument returns ALL posts.
    * @see https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
    */
    if ( $favorites ) :
    $favorites_query = new WP_Query(array(
    'post_type' => 'post',
    'posts_per_page' => -1,
    'post__in' => $favorites
    ));
    if ( $favorites_query->have_posts() ) : while ( $favorites_query->have_posts() ) : $favorites_query->the_post();
    // Treat this like any other WP loop
    endwhile; endif; wp_reset_postdata();
    // Treat this like any other WP loop
    endwhile; endif; wp_reset_postdata();
    else :
    // No Favorites
    endif;
  6. kylephillips created this gist Apr 18, 2015.
    19 changes: 19 additions & 0 deletions favorites-display
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    // This function will accept an optional User ID parameter
    $favorites = get_user_favorites();

    // Method 1: simple foreach loop
    if ( isset($favorites) && !empty($favorites) ) :
    foreach ( $favorites as $favorite ) :
    // You'll have access to the post ID in this foreach loop, so you can use WP functions like get_the_title($favorite);
    endforeach;
    endif;

    // Method 2: WP_Query Object
    $favorites_query = new WP_Query(array(
    'post_type' => 'post',
    'posts_per_page' => -1,
    'post__in' => $favorites
    ));
    if ( $favorites_query->have_posts() ) : while ( $favorites_query->have_posts() ) : $favorites_query->the_post();
    // Treat this like any other WP loop
    endwhile; endif; wp_reset_postdata();