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, multiple fields are separated with a semicolon.
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?

45 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.

  • Jos Velasco says:
    Hello,
    Nice plugin.

    Is it possible to filter a list having X AND Y values from more than one custom fields?

    For example, show movies where director is Woody Allen and format is blu-ray.

  • Hi Jos,
    the most recent version of the plugin (1.9) now has this feature and you can add as many key-value pairs separated with a semicolon; the key is still separated from the value with a comma.
  • Nicole says:
    Hi,

    This plugin is exactly what I’ve been looking for! I just have one question. Is it possible to have the list display the “featured image” above the post title? Not sure if that makes sense. I just want people to see a thumbnail of the post along with the post title.

  • Hi Nicole,
    since the plugin doesn’t provide an output that’s suitable for everybody you’ll have to adapt the part that displays the content as you see fit. If you know some PHP, HTML and the WordPress API that shouldn’t be a problem. You can contact me for professional help as well.
  • Hi Christian,

    The BEST Plugin ever…
    Question: I want a user to enter a value through a box in sidebar (which will be the field value) and based on this to click a submit button to show the list page…

    Is it possible?

  • Hi Alexandros,
    sure but you need some logic to extract the value the user entered and hand it over to this plugin. You’ll need some PHP for this and this can be as easy as the following code:

    echo lpcf_shortcode(array('field' => $_POST['user_field']));

    Given that the name of the input field in your sidebar is named user_field the above would display the list of posts with the given custom field; I haven’t tested this though.
    Please be careful with this approach because the plugin doesn’t check against any SQL injection attacks. You should think about validating your user’s input before handing it over to the plugin’s main function. By default, the plugin doesn’t check the user’s input due to the fact that the input entered via a shortcode should come from a trusted source in the first place.

  • bob33 says:
    THX Christian For the plugin
    Work out of the box…

    I cannot found solution when some of custom fied are set by the user. some are not filled

    Sur you have a solution for that…!

  • Jeff says:
    I have a blog with over 30,000 posts and this plugin seems to stall…anyway I can make it work with a blog of this size?

    Thank You. Great plugin!

  • Hi Jeff,
    you should make sure that the number of posts with a certain custom field that you’re selecting – with the field attribute – isn’t too large; I guess a few thousand posts should work though. Furthermore you can use the limit attribute: this way only that many posts defined via this attribute will be returned.
    If all that doesn’t work maybe you should drop the plugin and check whether you can archive the same functionality with the query_posts function. It allows you to select posts based on their custom fields too; meta_key, meta_value or meta_compare come to mind. This function call before the loop and a properly configured pagination may help you as well.
  • Greg Hill says:
    Hello Christian.
    Does your plugin work on WordPress version 3? I’ve just tried it and I don’t see the posts I expect.
    Thanks,
    Greg.

Leave a Comment