Support » Plugin: WP Favorite Posts » [Plugin: WP Favorite Posts] Sort favorite post

  • Resolved Colir

    (@colir)


    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/

Viewing 12 replies - 1 through 12 (of 12 total)
  • 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.
    ——————-

    yes thanks,
    this work great.

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

    thanks a lot

    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;
    			}
    		}

    // 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

    natcasesort() vs ksort() ?? need some test 🙂

    Plugin Author Huseyin Berberoglu

    (@hberberoglu)

    Thanks @pliote 🙂

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

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

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

    Also looking for some code so I can sort by category

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

    Awesome KTerceira, good luck 🙂

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘[Plugin: WP Favorite Posts] Sort favorite post’ is closed to new replies.