Support » Fixing WordPress » sort post by custom field for current category only

  • Hiya I’ve got lots of campsite addresses and I have entered them into custom fields. On my side bar I have split the campsites up into counties by using the category function. So I have a parent category for all campsites and a child category for the county they are in.

    This works fine and when you choose to view all campsites I have a listed returned with campsites grouped by town order. However to replicate this town order at child category level I am having to create a category template page for each child category – if I try and use the parent category it shows all the posts for every campsite.

    What I was wondering is – can I restrict the list only the current category I’m looking in.

    The code I am using for sorting by custom field is –
    <?php $values=get_post_custom_values(‘County’);echo $values[0];?>

    Hope this makes sense – any help would be really useful as I’ve googled this for ages and can’t seem to to sort it. I can either do it the long way or hopefully one of you great code people can help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • I’m not sure what you’re getting at. First let’s not mix up custom fields and (wordpress) categories here. As I understand you have the county value in a custom field. And then you want to only fetch the data of some county?

    If that’s so, I think it can be done like this (according to codex) :


    The only problem you have is how to dynamically get the right county based on what you clicked? Perhaps you could make a link and then using $_GET var retrieve to county? I’m not sure if wordpress can do it otherwise, anyway.

    Thanks for that but it will return the same results.

    I’ll try and describe this better – what I want is one category template (cat id 3) that I can then use by altering my functions.php to allow all the children to inherit that template – so from the outline below cats 11 and 12 would use the cat 3 template

    > Counties (Cat id 3)
    > Devon (Cat id 11)
    > Cornwall (Cat id 12)

    This works fine and will display only the posts from the child category using the cat 3 template – no problems. and I pull the posts by using the following simple piece of code –

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

    No what I’ve been trying to do is sort each of child category posts by town which is stored in a custom field by using this piece of code before the loop –

    <?php query_posts(‘meta_key=Town/City&orderby=meta_value&order=asc’); ?>

    This works fine for the parent category which shows all posts grouped by town alphabetically. However when I click on the child category it again shows all the posts from every county listed just like it did when you click the parent category.

    I think what I need is a statement that says if you are not in cat 3 then only show the posts from the child category you are in and then sort my custom field town.

    I may be asking too much here because of the inherit function – so I may have to bite the bullet and not inherit category templates and have a individual on for each child category. Which works how I want it too but I could end up having to create a few hundred, if I needed to make a styling change I’d have to then change every single child category template.

    Hope someone can understand and help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘sort post by custom field for current category only’ is closed to new replies.