1
votes

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.

1
how are you using custom fields with categories?superUntitled
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).humble_coder
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?superUntitled
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.humble_coder

1 Answers

1
votes

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(); ?>