Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi i100anni

    I use this way, first

    1. get the list of favorites
    2. if there are any favorites:
    1. create a WP_Query that posts ID are the same of favorites array
    2. iterate over result
    • else, there is no favorites, print message
    • $favorites = get_user_favorites();
      if ( $favorites ) {
          $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,
      	'orderby' => 'meta_value_num', // order by favorites count in this example
      	'meta_key'  => 'simplefavorites_count'
          ));
          if ( $favorites_query->have_posts() ) : while ( $favorites_query->have_posts() ) : $favorites_query->the_post();
              // here comes the loop
          endwhile; endif; wp_reset_postdata();
      } else {
          // no favorites yet
      }
    Thread Starter iamgosolo

    (@i100anni)

    thank you,

    but i need order my favorites order by addition

    ok?

    I have this problem too, but maybe tomorrow I will get some code for this. I need to test, but I think the array used to store post IDs in the meta table for the user is in the order of ‘clicks’.

    I hope my poor English can be understood by you…

    any progress on solving this problem?

    @mnlsr, if you want the order by addition in the favorites list, this will work:

    <ul>
    <?php
    global $wpdb;
    // in author's page, I show the last 3 favorites
    $favorites = array_reverse(get_user_favorites($author));
    $results = $wpdb->get_results('SELECT p.ID, p.post_title, p.post_name, FIELD(p.ID, ' . implode(',', $favorites) . ') ord
    FROM ' . $wpdb->posts . ' p WHERE p.ID IN (' . implode(',', $favorites) . ') ORDER BY ord LIMIT 3');
    foreach($results as $res) {
    	echo '<li><a href="' . get_permalink($res->ID) . '">' . esc_html($res->post_title) . '</a></li>';
    }
    ?>
    </ul>

    Thanks for the code. But where should we put this?

    dudaskank

    (@dudaskank)

    @mpatzekov, this will go in author.php of your (child?) theme.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to order favorist list’ is closed to new replies.