WordPress.org

Ready to get started?Download WordPress

Forums

WP Favorite Posts
[resolved] Sort favorite post (13 posts)

  1. Colir
    Member
    Posted 2 years ago #

    Hi,
    i would like to know if there a way to sort the favorite post listing by date, or by alphabetic order.

    I display the favorite post in a custom template page and i try to add this query
    ?orderby=title&sort=0

    but without effect...the post are always displayed by the order they've been add to favorite..

    thanks

    http://wordpress.org/extend/plugins/wp-favorite-posts/

  2. pilote
    Member
    Posted 2 years ago #

    for a sort by date you can sort by IDs
    sort()

    original: plugins/wp-favorite-posts/wpfp-page-template.php

    -------------------
    line16: if ($favorite_post_ids):
    line17: foreach ($favorite_post_ids as $post_id) {
    line:18: $p = get_post($post_id);
    etc.
    -------------------
    the same with "sort()":
    -------------------
    line16: if ($favorite_post_ids):
    line17: sort($favorite_post_ids);
    line18: foreach ($favorite_post_ids as $post_id) {
    line:19: $p = get_post($post_id);
    etc.
    -------------------

  3. Colir
    Member
    Posted 2 years ago #

    yes thanks,
    this work great.

    In the same way do you have an idea to sort this listing by 'title' ?

    thanks a lot

  4. Colir
    Member
    Posted 2 years ago #

    hi. thanks for your help.
    In fact, with the help of a developper,
    here is 3 differents sorting, base on 'added date to favorite', 'title', and 'rating'(need post-rating plugin)

    if ($favorite_post_ids):
    
    		$i=0;
    		if($_GET['orderby']=='date' || !isset($_GET['orderby'])){
    			$favorite_post_ids = array_reverse($favorite_post_ids);
    		}
    		if($_GET['orderby']=='title'){
    			$post_title_tab = array();
    			foreach ($favorite_post_ids as $post_id) {
    				$post_info = get_post($post_id);
    				$post_title_tab[$post_info->post_title] = $post_id;
    			}
    			ksort($post_title_tab);
    			unset($favorite_post_ids);
    			foreach ($post_title_tab as $title=>$id) {
    				$favorite_post_ids[] = $id;
    			}
    		}
    		if($_GET['r_sortby']=='highest_rated'){
    			$post_rating_tab = array();
    			foreach ($favorite_post_ids as $post_id) {
    				$post_info = get_post($post_id);
    				$post_rating_tab[get_post_meta($post_id, 'ratings_average', true)] = $post_id;
    			}
    			krsort($post_rating_tab);
    			unset($favorite_post_ids);
    			foreach ($post_rating_tab as $rate=>$id) {
    				$favorite_post_ids[] = $id;
    			}
    		}
  5. pilote
    Member
    Posted 2 years ago #

    // use $favorite_post_ids to populate new array(ID => title)
    if ($favorite_post_ids):
    foreach ($favorite_post_ids as $post_id) {
    $p = get_post($post_id);
    $favorite_post_by_title[$post_id] = $p->post_title; // new array
    }
    // sort by title and maintain index association [ natcasesort() ]
    natcasesort($favorite_post_by_title);

    // and then loop as usual using index as $post_id
    foreach ($favorite_post_by_title as $post_id => $title) {
    $p = get_post($post_id);
    // etc.
    }
    note: you have get_post() twice, not good for performance, but it work

  6. pilote
    Member
    Posted 2 years ago #

    natcasesort() vs ksort() ?? need some test :-)

  7. Huseyin Berberoglu
    Member
    Plugin Author

    Posted 2 years ago #

    Thanks @pliote :)

  8. tgiokdi
    Member
    Posted 2 years ago #

    I'm a little lost as to where to put this bit of code, and how to call it up.

  9. ZakS
    Member
    Posted 2 years ago #

    Im also a little lost as to where to put the code, am keen to sort them!

  10. KTerceira
    Member
    Posted 2 years ago #

    Can anyone advise how to sort by Parent Category or Category please

  11. igneous
    Member
    Posted 2 years ago #

    Also looking for some code so I can sort by category

  12. KTerceira
    Member
    Posted 2 years ago #

    I'm putting some cash together to have it done for me so when I do , I'll share the results, with you guys

  13. igneous
    Member
    Posted 2 years ago #

    Awesome KTerceira, good luck :)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic