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 by default a comma is used to separate the value from the field’s name, multiple fields are separated with a semicolon.
having_delim and having_delim2
In case your custom field contains the default delimiter that is either used to separate the key from the value or multiple custom fields from each other in the having clause you can change the delimiters. This would be an example to get all the posts having a field test set to either xxx or xxx,yyy: [lp field=test having="test;xxx|test;xxx,yyy" having_delim=";" having_delim2="|"]. Due to the fact that the value xxx,yyy contains the standard delimiter “,” I used a “;” instead.
Defaults are as follows: having_delim = “,” and having_delim2 = “|”.
having_conj
You can switch between and or or as the conjunction of multiple fields in the having clause; default is or. This way either all or just at least one field with it’s key / value combination needs to be present to add the post to the result set.
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?

86 comments ↓

  • Kaustubh says:
    Amazing Plugin Christian, very useful! Need you help, with a problem. My custom field name is “services”, the user has the option to select 1 or more predefined “service_values” [from a list of 25 services] while creating a post.

    I would like to show posts with a particular service selected, i tired using [lp field=services having="services,dentalimplants" orderby="post_title" order="asc" ] but it dose not work for me. When i use [lp field=services orderby="post_title" order="asc" ] it works but i am not able to filter results based on the key vakue. Could you please help, Thanks!

    Cheers,
    Kaustubh

  • Hi Kaustubh,
    I created two pages, both having a custom field named “services” with the value set to “dentalimplants”. If I then use the first shortcode you posted I get a list of these two posts. I tried adding a third post, added a custom field named “services” and set the value to “test” – this post doesn’t show up in the list. It seems to be working just nice.
    Please try to make sure that the value is set exactly to “dentalimplants” without any whitespace because the code the in plugin relies on an exact match of the string in the custom field compared to the one you enter in the shortcode. If you want to you can change this comparison to something else; see line 204 in the plugin’s file.
  • Luke says:
    Thanks for your plugin.
    If is possible displaying posts similarly as in the statement category (title, perex, preview image)?
    Now it looks like this http://www.outdoortipy.cz/jihocesky-kraj/
    but I would like this displaying http://www.outdoortipy.cz/kategorie/cestopisy/

    Thanks for you answer

  • Hi Luke,
    simply adapt the output produced by the plugin to suit your needs. You can open the file list-posts-custom-field.php and starting on line 190 you can change the HTML code that’s generated.
  • hankmadudi says:
    Ho Christ, thanks for the plugins.. it help me alot :)
  • Herman says:
    Hi… can we use this on our recipe site. We use the Cooking Lovers theme from Simon Bouchard. Unfortenately he doesnt give any support :( Its a nice theme but we need to have a sort of an index of all the recipes.

    thx

  • Hi Herman,
    sure, just use the plugin if it helps you creating an archive of all recipes on your site. It’s pretty easy to adapt the plugin’s output to your site’s needs so I’m sure it’ll help you with your index page.
  • Holby says:
    Hello

    Thank you for this!

    I want to sort a list alphabetically based on the last name

    I Have this: [lp field=last_name orderby= cfvalue:last_name order=desc ]

    The order does not come out alphabetical, is there anything I can do.

    Thank you!

  • Holby says:
    It works, I had made an input error. Thanks!
  • Denise says:
    Thank you for the great plugin. I can see that its going to be making my life much easier.

    I’m using this shortcode:
    [lp field=title_alpha having=title_alpha,O]

    but I would like to output a couple of custom fields along with the title. I’m trying to edit list-custom-post-field.php, but am not so great with php and can’t seem to get the right syntax…

    I’m looking at:

    $rVal .= '<dt>'.$post->title."</dt>\n".
             '<dd>'.$post->excerpt."</dd>\n";

    I’m struggling to add: . get_field('year_written') . (?) next to the title. I’m sorry to bother you with something that is probably easy to do, if I knew better what I was doing!! Any help is appreciated.

    Thank you.

  • Hi Denise,
    first off, I’m glad that the plugin is helping you on your website. If you want to insert the custom field year_written after the title the following code should do the trick:

    $year_written = get_post_meta($post->ID,
                                  'year_written',
                                  true);
    $rVal .= '<dt>'.$post->title.' '.$year_written."</dt>\n".
             '<dd>'.$post->excerpt."</dd>\n";

    You can of course add more custom fields as you see fit.

  • Denise says:
    That did it! Thank you thank you!!
  • Harry says:
    Hello Christian,

    I am having some difficulty in using the “having” filter option. I am using WP_User Frontend to write posts and there are options xxx, yyy and zzz for custom field “Type”. This way, the custom field name becomes cf_Type. The author can select one or more options and so the value for custom fields cf_Type could be either xxx yyy or zzz, but also xxx,yyy xxx,zzz or yyy,zzz and even xxx,yyy,zzz. If I want to display all posts that have either xxx or xxx,yyy or xxx,zzz or xxx,yyy,zzz on a page, how do I have to write the shortcode?

    Thanks for your great plugin, Harry

  • Octavio says:
    Amazing plugin! Thanks for sharing.
    I wonder if there is a way to build a list with the posts “having blah,123″ OR “having blabla,345″.

    thanks!

  • Nuria Serrat says:
    Thanks and congratulations for this plug-in!

    I’ve got an issue:

    [lp field=tipus having="ph,indiferent;alcada,de 3 a 7 metres"] and
    [lp field=tipus having="alcada,de 3 a 7 metres;ph,indiferent"] returns different results. Only the first one is correct, the second return all posts with tipus field. So I guess the order is important, but which order?

    Thanks…

  • Hi Harry, Octavio, Nuria,
    @Harry: the most recent version of the plugin allows you to change the delimiter used to separate keys from values; I updated the docs above as well. This way the following should help you [lp field=cf_Type having="cf_Type;xxx,yyy" having_delim=";"] to change the default delimiter from “,” to “;” so it doesn’t interfere with the “,” in the custom field’s value.
    @Octavio: the plugin allows you to add as many custom field combinations to the having clause separated by a “|”. By default these fields are logically ORed; you can change it to AND if you want to. Example: [lp field=blah having="blah,123|blabla,345"].
    @Nuria: I updated the code which evaluated the having clause so please try to test this again. The order of the fields in the having clause should be irrelevant.
  • AK says:
    This plugin is great!
    One question, as soon as i use split=”1″, the title changes to the field value. How can i set it so that the title remain the name of the post?
  • I just released an updated version (1.9.4) that should fix this issue. If it does not fix your particular issue I will need some more time to check this out in more detail.
  • AK says:
    1.9.4 didn’t fix the issue. However, i got around it by simple replacing this around line 377:

    $title = ( $split == true ? $post->cfvalue : lpcf_get_custom_field($post->ID, $titlefields, $post->title) );

    with:

    $title = $post->title;

  • Thank you for the feedback – great to hear that you could fix this yourself!
  • Johan says:
    Could this plugin be used to list Pages as well as Posts? It wasn’t clear to me from the documentation. I want to display a linked list of pages, like so: ‘Page title’, ‘Name of publication’, ‘Published date’ (i.e., not the Page’s creation date). Could that be done with the help of this plugin?
  • Dear Johan,
    by default the plugin already uses posts and pages when searching items with the given fields. Regarding the way how you want to output the data you can change the HTML markup directly in the plugin’s PHP file starting on line 228; for special things you may need to know the WordPress API a bit though.