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 ↓

  • Mario says:
    Moin,

    ab 3.1 funktioniert dein Plugin leider nicht mehr, hast du da ne Lösung?

    VG
    Mario

  • Hi Greg and Mario,
    I’ve tested the plugin with the most recent version of WordPress (3.2.1) and everything seems to be working fine. If you’ve got a specific issue with the plugin on your installation please contact me via the contact form and let’s find a solution.
  • tigerkater says:
    hallo christian,

    wieder muss ich mich herzlich bedanken für das tolle plugin. eine frage habe noch zum excerpt/excerptfield: besteht die möglichkeit die normale excerptfunktion von wp zu verwenden/implemenrieren? (the_excerpt) – ich habe versucht ein bisschen am code zu basteln, bin aber gescheitert :(

    wenn ich dich richtig verstehe, müsste man zuerst ein excerptfield bestimmen oder erstellen und füllen, erst dann wird ein excerpt angezeigt.

    vielleicht wäre es ja nicht nur für mich toll, wenn in einer zukünftigen version des plugins die option besteht, auch das normale wp excerpt anzuzeigen…

    herzliche grüsse

    tigerkater, für jede hilfe dankbar!

  • Hallo tigerkater,
    es ist eigentlich viel einfacher: die Angabe von excerptfield weglassen und das Standard-Exzerpt wird genutzt; Tabelle wp_posts, Spalte post_excerpt. Natürlich kann man dies auch im Code hartkodieren, dann aber bitte get_the_excerpt nutzen.
    Ein Mischbetrieb zwischen einem Exzerpt in einem benutzerdefinierten Feld im einen Post und dem Standardexzerpt im anderen Post ist aktuell tatsächlich nicht möglich.
  • Coolpix says:
    Can we have thumbnails on the post lists? I found this plug-in very interesting, but I don’t see how we can have thumbnails showing. Please let me know if I am doing something wrong.
  • Hello Coolpix,
    by default the plugin doesn’t have a feature that allows you to show thumbnails in front of each result. In case you know PHP please adapt the output of the plugin to include the desired images which should be pretty straight forward. If you would like to contribute this change to the original plugin simply get in touch with me.
  • Nathan Little says:
    Hi there,
    Can you please write an example of a full line of code showing the “having” function being used? I keep getting “No custom field found. Add it to the shortcode. ”

    Thank you!

  • Hi Nathan,
    this message says that you should add a custom field to the shortcode like so:

    [lp field=test having=blah,123]

    This would retrieve all posts with the field test having another field called blah which is set to the value 123.

  • The standard way that WP works – showing items in the order that they were posted to the system – meant that they were displayed in the wrong order on a page of events which I wanted listed in the order that the events would occur. Editing the posted date – which was a solution suggested by some – did not change the display order.

    To overcome this problem, I added a custom field for the dates of events. Your plugin now tests if the field is used and, if it is, creates the correct output showing a list of upcoming events in the date order as entered in the custom field.

    Once the event is over and I do not want to display it any longer, I delete the data in the custom field. Unfortunately, the plugin still shows the entry even though the field is now blank.

    Is there a way to test the value of the data in the custom field and only display posts where the tested value meets certain criteria? For example, only show items that have a date after today’s date?

  • Kathryn says:
    Thanks for this useful plug-in. For the most part it works great, but when I try to give a post more than one custom field so it appears in more than one section, I can’t get it displaying correctly, even when using split=1.

    For example, the following does *not* display a post in both sections – it actually suppresses display of all posts. Let’s say a post has the custom field “overview” with the value of “another-theme” *and* “great-work” and I want the post to appear in both of the lists:

    [lp field=overview orderby="post_title" having=overview,another-theme split=1]

    [lp field=overview orderby="post_title" having=overview,great-work split=1]

    Is this the correct implementation? Removing split=1 causes the posts to be displayed correctly, except that a post in multiple sections is only displayed in a single section, not both.

    Any assistance appreciated. Thanks in advance.

  • Usama says:
    Is it possible to sort the post according to custom field like display with custom field?
  • Hi Phillip, Kathryn and Usama,
    @Phillip: the behavior you’re describing seems to be really anoying although I couldn’t reproduce it on my end. Answering your question: sure it would be possible to add a certain method that would allow us to compare a custom field and some other value and maybe I can add this in a future version.

    @Kathryn: this was in fact a bug in the plugin and version 1.9.1 should fix this. The split attribute is used in another context, simply leave it out and the above code should be working for you just as expected.

    @Usama: you can use orderby and order to alter the order of the posts.

  • Raffaell says:
    Hi,

    I’ve been using this plugin and seems like its very nice to adapt. I have 1 problem in terms of using having.

    If I only use having it will be error:
    No custom field found. Add it to the shortcode.

    [lp having=cp_state,"Kuala Lumpur" orderby="post_title" order="desc"]

    But if I use:
    [lp field=cp_city having=cp_state,"Kuala Lumpur" orderby="post_title" order="desc"]

    It will show me all the post with field cp_state but not only Kuala Lumpur.

    What I need is, I need to create pages for every state in the country, and I would like to list all the post that related from the state based on the custom field cp_state.

    How to do that ?

Leave a Comment