How do I sort by a custom field without manually creating a new page?

Having a bit of an issue with WordPress here. In all honesty I’ve always designed my sites from scratch and “coded” from the ground up. Lately I’ve been trying to work with WP as I’ve heard good things about it.

It would appear that WP gives you many things for free (e.g. dynamic “pages” based on CATEGORIES). However, I would like to know how to manipulate these freebies without reinventing the wheel. For example, I would like to have my SUB-MENU display a list of post categories. But I would like to sort those categories by a CUSTOM FIELD.

Read More

Now, I could reinvent the wheel and manually create (and link to) a new page for each sort, so on and so forth, (which I don’t fundamentally mind doing) however, I’m hoping there is a way around this via plugins or otherwise. I’ve seen several tutorials on custom queries, but they stop short of implementation — they simply give the query without telling exactly whether to create a new page or plug it into a function somewhere.

Any input would be most appreciated.

Best.

Related posts

Leave a Reply

1 comment

  1. At the top of category.php template in your theme’s root directory, add the following to add your custom sort field to the query:

    <?php
    function is_valid_custom_sort_field($field)
    {
        // implementation left as an exercise for the questioner
        return true;
    }
    if ($_REQUEST['sort_custom_field'] && is_valid_custom_sort_field($_REQUEST['sort_custom_field'])) {
        query_posts($query_string . '&orderby='.$_REQUEST['sort_custom_field']);
    }
    

    See:
    http://codex.wordpress.org/Function_Reference/query_posts

    If your theme doesn’t have a category.php, here is a simple default template to base it on (copied from the included twentyten theme):

    <?php
    /**
     * The template for displaying Category Archive pages.
     */
    
    get_header(); ?>
    
            <div id="container">
                <div id="content" role="main">
    
                    <h1 class="page-title"><?php
                        printf( __( 'Category Archives: %s', 'twentyten' ), '<span>' . single_cat_title( '', false ) . '</span>' );
                    ?></h1>
                    <?php
                        $category_description = category_description();
                        if ( ! empty( $category_description ) )
                            echo '<div class="archive-meta">' . $category_description . '</div>';
    
                    /* Run the loop for the category page to output the posts.
                     * If you want to overload this in a child theme then include a file
                     * called loop-category.php and that will be used instead.
                     */
                    get_template_part( 'loop', 'category' );
                    ?>
    
                </div><!-- #content -->
            </div><!-- #container -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>