WordPress List Posts with Custom Field Plugin

This plugin provides a shortcode that displays a list of posts with a certain custom field. Since you can supply multiple custom fields you’re able to aggregate posts with different custom fields. Furthermore you can easily change the sort order.

You can download the plugin here.

Howto

Once you’ve installed the plugin you can use the shortcode list-posts (or lp for short) to display posts. Say, if you wanted to get all posts with the custom field howto you would enter this into the content of a post/page:

[lp field=howto]

This results in a list of posts with the given custom field.

Attributes

You can use the following attributes in the shortcode.

field
You can supply the custom field here. If you’d like to use multiple custom fields just separate them with a comma.
orderby
By default the posts will be ordered by their publication date. If you’d like to order them by their title you would use post_title. Look at the fields of the wp_posts table and pick whatever you like. Ordering by the value of a custom field is possible too: just use cfvalue:field here and replace field with the real key of the custom field that should be used during the sorting algorithm.
order
Either use ASC or DESC for ascending or descending order respectively.
titlefield
The plugin uses the standard title of a post to display the title but may use a custom field instead. This comes in handy if you want to display another title for a post in the list generated by this plugin without chaning the post’s original title.
excerptfield
Again, this field lets you choose a custom field which will be used instead of the post’s excerpt. Say, you’re using the All in one SEO plugin which stores the meta description in a custom field called description you may use its value instead of the standard excerpt.
titleprefix, titlesuffix
The title that’s displayed can have a custom prefix or suffix. The constant %PERMALINK% will be replaced with the permalink of the current post or page.
excerptprefix, excerptsuffix
You can specify a custom prefix or suffix for the excerpt as well.
split
In case you specified a custom field via the attribute field that may be attached multiple times to a single post or page you can split the results by turning on this attribute, e.g. with split=1. This way the output may contain a post or a page several times.
category
You can make sure that the results will be associated with the given categories. Just supply a comma separated list of category IDs.
having
This attribute helps you displaying posts that have a given custom field set to a specific value. It’s a great way to display all posts having a certain custom field and another one with a particular value. Say, you only want to display posts having a field blah with its value set to 123 you would use having=blah,123 in the shortcode; see that a comma is used to separate the value from the field’s name.
limit
Helps you to limit the amount of displayed elements. If you’ve got five posts with a particular custom field but only want to show the first three you’d use limit=3.

Examples

I’ve created separate pages for all my WordPress plugins. All of them have a custom field called wordpress. The following shortcode was used to produce the list as you can see it here:

[lp field=wordpress orderby="post_title" order="desc"]

If I create a new page for a plugin I just have to annotate it with wordpress and the list will display it automatically. Neat, isn’t it?

22 comments ↓

  • Just found your plugin and I would like to use it to list my article sorted by author. Rigth now I am getting a list of all articles. How can I have a headline of the author and beneath the articles that belongs to them.

    My custom field is author and I use it to store the author of the article.

    Thomas

  • Hi Thomas,
    the plugin doesn’t support this because it had to look at the content of the custom field and compare it to some pre-defined value. Of course, you could adapt to plugin to do this but I think you’re better off using the function wp_list_pages along with the parameter authors.
  • tigerkater says:
    hi christian,

    ich wollte dein plugin installieren, um gewisse beiträge auf einer statischen seite anzuzeigen und eventuell mit einer anderen überschrift zu versehen.

    leider war ich nicht erfolgreich. ich habe dem gewünschten beitrag ein benutzerdefiniertes feld hinzugefügt (schlüssel=field, wert=rezept). auf meiner statischen seite im editor [lp field=rezept orderby="post_title" order="desc"] eingegeben, aber leider ohne erfolg.

    ich bin nicht besonders versiert, also vielleicht mache ich wo einen fehler. hast du ihn entdeckt?

    im prinzip würde ich genauso, wie du auf deiner pluginsseite eine bebschriebene liste erstellen wollen. hoffentlich klappt es mal.

    danke dir und grüsse

    horst

  • tigerkater says:
    hi christian,

    bin schon eine spur weiter: der wert schlüssel muss den field wert enthalten (also z.b rezept).

    wie kann ich aber die beiden felder excerptfield und titlefield anzeigen und auslesen lassen?

    danke und grüsse horst

  • DarioDN says:
    It is possibile order the list by a value of a custom field?
    now I can order by post_title (i.e.) but I would order by a value of a particular custom field too.
    tnx
  • Hi DarioDN,
    this is on my todo list and I’ll add it to the plugin in a future version.
  • Mike says:
    Is it possible to modify this to only list posts with a specific custom field.

    ie; the plugin will look for all posts with a custom field of Blah.

    I want it to list only posts where Blah = 123 (for example)

    Is that possible? I assume it’s just a modification to the code, but I can’t figure out where.

  • Hi Mike,
    search for the first foreach in the plugin’s file. Inside this loop you’ll have to add code like the following:

    $blah = get_post_meta($post->ID, 'Blah', true);
    if ($blah != '123') continue;

    This should make sure that only posts having a custom field “Blah” set to “123″ will be displayed.

    Update: This can be done with the having attribute now.

  • Richard Bui says:
    Excellent plugin! One question I had is: would there be a way to limit the amount of results displayed? For example, the first 5?

    Thanks!

  • Hi Richard,
    I’ve added another attribute: use e.g. limit=5 to do just that.
  • Richard Bui says:
    @Christian: you are amazing! I sent you an email, BTW. Thanks!
  • @nomadone says:
    Wow this sounds great. I’ve been battling to print a set of posts, with a specific custom field value, but need to post them inside of another post. Is there any way of using this plugin with a template tag instead of shortcode though?

    I’ve just tried using the plugin and it basically adds all custom fields from the specific field not just the ones with the value I specified.

  • masterleep says:
    I’ve been using the plugin with a placement like this:

    [list-posts field=xyz having=xyz,none]

    When I upgraded to WordPress 2.9.1 from 2.8.x, the page changed to a list of “Array” strings. Instead of the post title, the string “Array” would be there.

  • masterleep says:
    FYI, I was able to patch it for my purposes by commenting out the following section in the plugin:

    # set correct title/excerpt
    #for ($i = 0, $n = count($result); $i title = lpcf_get_custom_field($post->ID, $titlefields, $post->title);
    # $result[$i]->excerpt = lpcf_get_custom_field($post->ID, $excerptfields, $post->excerpt);
    #}

  • Hi masterleep,
    thank you for the feedback. I’m looking forward to fix the plugin.
  • Adam says:
    Very nice, simple plugin.

    2 questions:

    1. In many posts, I have the same custom field key with different values. Your plugin seems to recognize this, but only lists the value in the first instance of the key. Is there a way to solve this?

    2. I think that this has been mentioned before, but is it possible to sort by the value of the custom field?

    Thanks,

    Adam

  • John Hayles says:
    Hello,

    Is it possible to combine this plugin with a WP category shortcode?

    If I have a number of posts about different airlines in the category ‘airlines’, with a custom field holding the airline name. To write a post that includes a list of these articles I need to combine the shortcode for list-posts and a shortcode for the category.
    John

  • Hi John,
    I may include another attribute category that would allow you to use those posts only that are part of the given category. This would do the trick for you, I think.
    Update: version 1.8 of the plugin contains this feature.
  • John Hayles says:
    What about when the field value is two words, what is the syntax?

    having=blah,”12 34″ does not appear to work.

  • Hi John,
    please enclose the whole value of the attribute having in quotes.
  • Simply a great plugin. Thanks for sharing.

    Is there a way to maintain the permalink in the title and still prefix with <h3>? Thanks.

  • Hi David,
    unfortunately I haven’t documented this but here’s how you’d do it. Add the following to the shortcode:

    titleprefix="<h3><a href='%PERMALINK%'>" titlesuffix="</a></h3>"

    The special string %PERMALINK% will be replaced with the corresponding permalink.

Leave a Comment