Support » Plugins and Hacks » List posts by Key

Viewing 15 replies - 1 through 15 (of 33 total)
  • So you’d want a get_posts function that can select only posts of a custom field and sort by them. Don’t know that one exists but a plugin could be made for it. C2C has a customizable post plugin, but it doesn’t do custom fields.

    Coffee2Code also has a Get custom fields plugin, but that has nothing to do with your request. If I get around to making a plugin to do this, I’ll let you know.

    You know you can specify a key with the Search Custom Fields plugin you mention above?
    http://guff.szub.net/2006/04/21/search-custom-fields/

    I wonder if I could hack that plugin to produce a “smart search” based on Key? Like every time you go to the “book index page” to see the list of books reviewed, it is actually outputting the search results for the key BookTitle.

    Using Kaf’s plugin, maybe I could use link with a search string. Like:

    <a href="http://mydomain.net/?s=plugin&key=BookTitle">Index of books reviewed</a>

    This could be an interesting workaround…

    <del>Unfortunately I would like the results in alphabetical order. </del>

    Whoops!

    The other thing is that Kaf’s search doesn’t output the content of the Custom Fields, just the names of the post. His results are:

    CustomFieldsKey : PostTitle
    PostDate Cat Comments

    I would like:

    CustomFieldsContent [eg. BookTitle or AuthourName] : PostTitle
    PostDate Cat

    There doesn’t seem to be a function that can call the contents of a Custom Field.

    When you call the loop as usual on a search.php page, you can have posts display the content.

    I presume you are simply looking at these results to determine what can be done:
    http://guff.szub.net/?s=plugin&key=wp

    The content is formatted that way only because that is how he’s formatted the loop on search.php.

    Because ‘s’ is set in the query string, WP thinks it is a regular search, so by the Template Hierarchy, it checks for a search.php in your template folder to display the resulting posts for that search, which with the help of the plugin, also searches for posts according to the post metadata. The data written for each post is like that of any loop on your index or sidebar templates.

    Geoffe,

    Thanks for following up on this with me.

    The content is formatted that way only because that is how he’s formatted the loop on search.php.

    I understand that. I tried to set up a custom search.php page which so far looks like this:

    <?php while (have_posts()) : the_post(); ?>
    <?php query_posts('orderby=title&order=ASC'); ?>
    <div class="post">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a> - <small><?php the_time('l, F jS, Y') ?></small>
    </div>
    <?php endwhile; ?>

    But I don’t want an alphabetical list of post titles, I want an alpha list of book titles which are contained in the Custom Field values. I can’t figure out what to replace <?php the_title(); ?> with so it will call the Custom Field values. Maybe get_post_meta($post_id, $key, $single) without anything in post_id will return all the results.

    Any ideas?

    you might need to use the get custom fields plugin to get the bit you need..

    not to worry, anything can be done. You just need to sort the results by meta_value.
    And then you can call the meta in the loop with the_meta, or get_post_meta as you suggest but to sort you’ll need to add this filter into the plugin:

    add_filter(‘posts_orderby’, ‘meta_value’);

    I think you should add it in the line above the
    return $where;
    line inside the szub_search_custom_where() function in the plugin.

    The custom fields plugin seems to (almost) be able to handle this, in a much more elegant way than the “smart search” approach. I can now produce a list of BookTitles on a custom-archive.php page with the following code:


    <?php query_posts('orderby=title&order=asc&showposts=-1'); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php echo c2c_get_custom('BookTitle', '<li>', '</li>', '', '', '' ); ?>
    <?php endwhile; ?>
    <?php endif; ?>

    So far this is great, but now the problem is creating links to the actual reviews of the books.

    I would like to stick the following in there somewhere:


    <a href="<?php the_permalink() ?>" style="text-decoration:none;" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
    <?php the_title(); ?>
    </a>

    … but unfortunately it seems when c2c_get_custom grabs the field values, it doesn’t have any info on the actual post the field values are associated with. Unless this has something to do with the Loop, or with the query.

    But what you are doing is looping through posts while calling only the BookTitle meta information, if I understand correctly.

    If you put the_permalink() into the loop, it should produce the permalink for each post including the ones that don’t have BookTitle meta information. There isn’t anything in your loop or restrictions to keep the posts to only posts that have BookTitle meta info.

    And doesn’t ordering by title just order by the title of the post? If you put the add_filter(‘posts_orderby’, ‘meta_value’); line into the search plugin, it will set the order of posts by meta_value, which will be the Book titles if the meta_key is BookTitle. Or did you try that and it didn’t work?

    If you put the_permalink() into the loop, it should produce the permalink for each post including the ones that don’t have BookTitle meta information.

    That is exactly what happened, disappointingly.

    If you put the add_filter(‘posts_orderby’, ‘meta_value’); line into the search plugin, it will set the order of posts by meta_value, which will be the Book titles if the meta_key is BookTitle. Or did you try that and it didn’t work?

    It returned an error when I tried to put it in.

    Actually, I am lucky since I have a specific “Reviews” (cat=2) and “Interviews” (cat=4) categories, so I can limit the query to these cats specifically and use the_permalink. The last thing now is ordering. I tried to use meta_value and the_meta to no avail.

    Here is what I have done so far:

    <?php query_posts('orderby=meta_value&order=desc&showposts=-1&cat=2'); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <a href="<?php the_permalink() ?>" style="text-decoration:none;" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php echo c2c_get_custom('BookTitle', '', '', '', '', '' ); ?></a>

    <?php endwhile; ?>
    <?php endif; ?>

    Just realized why my solution in ordering by meta_value can’t be done — because it’s not in the selected values of the SQL query. [Which is also why it won’t work for your ordering in the loop.]

    You’ll need to make a unique query for posts in order to get your posts sorted by meta_value. The default is to select all fields from the posts table but you’ll need a join and select from postmeta table.

    Here we go. Try using this:
    <?php
    if ( $recentposts =
    $wpdb->get_results(“SELECT ID, post_title, post_date, post_content, meta_value, guid FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id WHERE post_status = ‘publish’ ORDER BY meta_value DESC LIMIT 5”)) {
    foreach ($recentposts as $post) {
    echo “{$post->meta_value} – < a href=\”{$post->guid}\”>{$post->post_title} – {$post->post_date}
    “;
    } } ?>

    Thanks for that Geoffe. Is there something I have to fill in here? It seems to capture the wrong metadata. Since I have wp_texturize installed, the output is a big unending block like this:

    wptexturize – < a href=”permalink”>PostTitle – TimeDate

    Where should I put in BookTitle in that code snippet of yours?

Viewing 15 replies - 1 through 15 (of 33 total)
  • The topic ‘List posts by Key’ is closed to new replies.