• So in my sidebar I am listing my pages:
    <?php wp_list_pages('title_li=<h2>Your City</h2>' ); ?>
    Pretty simple however, what if I have a custom field on every page and I want to sort based on that custom field.

    Example. I have 150 cities as page. Each City has a custom field called state. I want to sort it alphabetically by state then city.

    Next I want to indent all the cities. Normally this would be simple in php but I’m not sure where all the data is and how to do this with php

Viewing 2 replies - 1 through 2 (of 2 total)
  • Displaying Posts Using a Custom Select Query might head you in the right direction.

    Thread Starter gbrent

    (@gbrent)

    Michael,

    Yeah that’s what I figured out while I was looking at the DB. For others here is what I did…
    I went through each Page that was a city and I added the custom field ‘state’ then I did the following query:

    SELECT DISTINCT p.post_title, p.post_name, p.guid, m.meta_value FROM wp_posts p
    LEFT JOIN wp_postmeta m ON (p.ID = m.post_id)
    WHERE meta_key = ‘state’
    AND post_status = ‘publish’
    ORDER BY menu_order desc, meta_value ASC

    Now I have not done it yet but when you l0op over this dataset you need to compare the meta_value in the current loop with the meta_value from the previous loop. If they are different then you print it with Bold or indent the cities etc.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Sorting pages by a custom field’ is closed to new replies.