• Hi,
    I was wondering if someone could show me how to sort posts based on meta data ‘year’ in a specific category? and print something like

    2011
    – post 1
    – post 2

    2010

    – post 1
    – post 2

    2009

    – post 1
    – post 2

    Also, what is the best way to create sort other options like sort by “title”, “post date” and etc in archive page.

    thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi radiofranky,

    There is http://codex.wordpress.org/Function_Reference/wp_get_archives in which you can specify ‘yearly’

    I also wrote a function to give better control and to display it in a different way, as I wanted to create an accordion menu of posts by year (so I could show the latest year, and hide the rest but allow them to slide out on click). As well highlighting and showing posts from only the current year.

    I pasted the code here http://pastebin.com/41QLxMT0 for anyone to use. Just place it in your functions.php file in your theme.

    To invoke use the following:

    edge_subcat_posts_monthly($post->ID, 24);

    Where $post->ID is the current post (I use this as I put this in the sidebar of single posts), and ’24’ in this instance is the category.

    Hope this helps out.
    Har

    Thread Starter radiofranky

    (@radiofranky)

    actually I would like to use my meta field ‘year’ to sort the posts, not the year that posts were posted.

    for example

    2011 movies
    – abcd
    – efaz

    2010 movies
    – 3jidj
    – djskj

    the thing I would like to have is to have one loop that sorts everything. I know I could achieve this by setting loops for each of the years but it’s not efficient.

    basically what I need is add something in the following code that prints out the year of each of the year groups.

    Print the first “year” and loop finished all the post with meta of the same year and print the “next year” posts

    here is the code ‘<?php
    $the_key = ‘year’; // The meta key to sort on
    $args = array(
    ‘meta_key’ => $the_key,
    ‘orderby’ => ‘meta_value’
    );
    global $wp_query;
    query_posts(
    array_merge(
    $wp_query->query,
    $args
    )
    );
    ?>

    <?php while (have_posts()) : the_post(); ?>’

    Hey did you even found how to do that?
    I’ve searched so much for the same function and can’t seem to find anything. I want to sort my custom post archives (which happen to be events) the same way… but want to categorize them by year first before displaying them as lists.

    Any help is much appreciated.

    you can use the get_post_meta() function to pull all meta data from posts in a loop.

    http://codex.wordpress.org/Function_Reference/get_post_meta

    You can also add meta key and value arguments to the query_posts() function in the loop. Something like

    query_posts( 'meta_key=year&orderby=meta_value' );

    but if it’s on an archive page and not a custom loop, you need to preserve the results of the initial loop which should be saved in the $query_string variable. So attach that to the above code like this and give it a try…..

    query_posts($query_string . '&meta_key=popularity&orderby=meta_value');

    thanks wspencer – just what I was looking for

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘how to customize archive.php to display list posts based on meta info "year"’ is closed to new replies.