WordPress.org

Ready to get started?Download WordPress

Forums

Sort (alphabetize) posts by custom field (11 posts)

  1. abrupt219
    Member
    Posted 8 years ago #

    I would like to sort posts in alphabetical order according to a particular custom field. After extensive searching of both WordPress and Google I've come close, but no cigar.

    As far as I can tell, the query_posts tag only accepts built-in values like title and date for the orderby attribute, but not custom fields. I do have the c2c_get_custom plugin, which is great, so any solution to my question can assume that as a baseline.

    Thanks for any ideas. I can provide a more detailed description of exactly why I need this, but it doesn't seem relevant at this point.

    Regards,
    abrupt219

  2. Hopefully this will do it. The drawback to this as it requires a query per post, but at least it's fairly simple. As a more complicated method, we could query all posts, stick them into the array with the post ID as the array key, and then using the meta query, output the array in order. But this will do for now...

    $postsmeta = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'your meta key name' ORDER BY meta_value");

    foreach ($postsmeta as $postmeta) {
    $post = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID = " . $postmeta->post_id);

    // Do your stuff here

    } // End foreach

  3. abrupt219
    Member
    Posted 8 years ago #

    Viper007Bond, thanks for the input. I've been playing with the above suggestion, but it's not exactly working. It seems that nesting the foreach loop inside (or outside) the Loop is resulting in way to many iterations. The page in question is just a list of post titles and custom field values, and what I'm getting is about 20 copies of each post's custom field value, without the title or other post data.

    I've tried changing the placement of the foreach statement, and even removing the original Loop function with various incomplete results. Where would you place your code in relation to the Loop?

    Perhaps your proposal for the "long version" is needed -- where the custom post ordering is read into an array in the beginning, then passed to the Loop for normal processing. This would seem to reduce the amount of querying going on, as you said, and might be more logically modular (ie create the query array, then process it).

    I should note that I'm teaching myself php and MySQL as I go along here, with no formal training. So I'm not really a newbie but certainly not an expert. I understand what your code above is attempting to do, but not quite why it's not working...

    Anyway, thanks for having a go at this one,

    abrupt219

  4. No, make that The Loop. No need for The Loop when using the above code. Just put your Loop contents where it says // Do your stuff here.

  5. abrupt219
    Member
    Posted 8 years ago #

    Well, I've spent all day hacking at it, and although I've gotten closer, I'm feeling that this is more trouble than it's worth. For one, establishing the foreach loop disrupts the functioning of the default in-Loop tags, such as the_permalink. While I found substitutes for some of these using array queries, the result is moving further and further away from manageability. Further, this is all occurring on a custom category template, so the pagination and other features are getting disrupted.

    Although it was a noble technical challenge, for what I'm doing it's truly not worth going further unless another avenue opens up.

    To give some background, I'm doing a "library" page, which basically has reviews of books I've read. For each post, I put the book author's name in a custom field called 'bookauth' -- obviously the default author_name field value is me, as the author of the post. I was trying to sort by the book author.

    Perhaps there is a way within the Loop to temporarily replace the value of the default author_name field with the value of the custom 'bookauth' field. Then the query_posts function could be used to sort by author_name. Seems sneaky, but hey -- I'm surprised what I'm asking to do isn't easier in the first place!

    Thank you once again for your suggestions. I've definitely learned a lot about arrays and queries in the process, and haven't lost hope for a working solution...

  6. abrupt219
    Member
    Posted 8 years ago #

    Oh, and by the way, the page in question can be enjoyed here.

  7. ericmcgregor
    Member
    Posted 8 years ago #

    Hi, I'm having a very similar problem. I would like to numberically tag a post using the meta data, and then sort the posts by that field. That way, I can re-order the posts in the future instead of being constricted to the date they were posted.

    Any thoughts on that? I'm not sure I'm understanding how the example above would work.

    Thanks.

  8. ericmcgregor
    Member
    Posted 8 years ago #

    Oh, also, the posts I want are category specific. ;)

    Interesting problem. I wish wordpress were better able to handle stuff like this. If I find anything I'll be sure to post it up here.

  9. thesheep
    Member
    Posted 7 years ago #

    I believe if you use

    setup_postdata($post);

    At the start of each iteration of the loop, this will set things up so you can just use the normal WP tags.

    I don't think it will help with pagination though :(

    See http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

  10. the_fear66
    Member
    Posted 7 years ago #

    This method has worked really well for me in using a custom query from a new table I set up in the WordPress DB. However when I use the get_post_custom_value function I get an error.

    my code looks like this:


    <?php
    $pageposts = $wpdb->get_results("SELECT *
    FROM wp_posts, wp_users, wp_myart, wp_post2cat, wp_categories, wp_postmeta
    WHERE wp_posts.id = wp_myart.post_id
    AND wp_myart.user_id = wp_users.user_login
    AND wp_posts.id = wp_post2cat.post_id
    AND wp_post2cat.category_id = wp_categories.cat_id
    AND wp_posts.id = wp_postmeta.post_id
    AND wp_myart.user_id = '$user_login'
    GROUP BY wp_myart.post_ID
    ORDER BY wp_myart.date_added DESC");
    ?>

    <?php if ($pageposts): ?>
    <?php foreach ($pageposts as $post): ?>
    <?php setup_postdata($post); ?>

    <div class="myart">
    <h3>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></h3>
    <img src="<?php the_excerpt_reloaded(); ?>" alt="<?php the_title(); ?>" />

    Price £<?php foreach (get_post_custom_values('Price (sterling)') as $value) {
    echo $value; }?>

    </div>

    <?php endforeach; ?>

    <?php endif; ?>

    and the error doesn't like the additional foreach by the looks of it. I can understand why it might - any other way of printing out this custom field??

    Warning: Invalid argument supplied for foreach() in /home/interart/public_html/dev/wp-content/themes/default/my-art.php on line xx

    thanks all
    Matt

  11. the_fear66
    Member
    Posted 7 years ago #

    I've been a div - i realised it was actually much more sensible to store this extra data in the postmeta table... and lo and behold it now all seems to work.

Topic Closed

This topic has been closed to new replies.

About this Topic