Sorting query results by value of custom key
-
Hi, I’m trying to create a recipe list for a food blog. I’ve written a query that returns the titles of posts within a category, or if the post has a custom field called “recipe_name” then it shows the value of that instead–this is because most posts take their name from their recipe, but not all. That much works (code below). The problem is that the list comes out sorted by post title only; posts appearing as the custom value are out of order. Any idea how to sort these by whichever value is being returned? Thanks for any help!
Example of code so far:
<ul> <h3>Dessert</h3> <?php query_posts('category_name=Dessert&showposts=-1&orderby=title&order=ASC');?> <?php while (have_posts()) : the_post(); ?> <li><a href="<?php the_permalink() ?>"> <?php $posts_recipe_name = get_post_meta($post->ID, 'recipe_name', true);?> <?php if(!$posts_recipe_name): ?> <?php the_title(); ?> <?php else: ?> <?php echo get_post_meta($post->ID, 'recipe_name', true);?> <?php endif;?> </a></li> <?php endwhile;?> </ul>
Viewing 13 replies - 1 through 13 (of 13 total)
Viewing 13 replies - 1 through 13 (of 13 total)
- The topic ‘Sorting query results by value of custom key’ is closed to new replies.