1

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.

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.

4
  • how are you using custom fields with categories? Commented Jan 12, 2011 at 2:34
  • I'm not. But I would like to. My apologies for not saying so, but I am using a custom theme: AgentPress( studiopress.com/themes/agentpress). Notice the sub-menu in the demo. I have a similar setup. However, once I click on one, I would like to have those values sorted by a custom field that is provided by the theme (e.g. price, location, zip code, etc). Commented Jan 12, 2011 at 3:19
  • Sorry, but I am still not really understanding... are you wanting to order the posts that are displayed when you click the category, or do you want to order the categories in the drop down menu? Commented Jan 12, 2011 at 6:29
  • Again, my apologies. I want the posts to be displayed after clicking to be ordered by custom fields. For instance if I have a field named "price", I would like to be able to sort by this field. Or, if I have a field named "color", I would like to be able to sort by this field. And I'd like to use the existing results without having to manually create a page for each iteration if at all possible. If not, I'm ok with that, but I'd still like to know the best approach in general. Commented Jan 14, 2011 at 6:00

1 Answer 1

1
+50

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(); ?>
Sign up to request clarification or add additional context in comments.

5 Comments

So I absolutely have to create a page manually? I can't use the links provided by "Custom Menu" to sort the results the way I want them?
I mean the existing category.php (woops, not categories.php) template in your theme's root directory.
Unfortunately the theme I'm using (AgentPress) has no "category.php" in its root -- although, for the record, I do see the file in other available themes. That said, is it, perhaps, falling back on another "category.php" file? Should I manually create the file inside the "AgentPress" theme folder? Is there a basic template to follow?
It has a fallback in case that file doesn't exist ion the current theme. I will update my answer with the default.
Excellent! I have yet to try it, but you've earned the points just for taking the time. Much appreciated! UPDATE: Apparently I can't award the points for another 21 hours :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.