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 thewp_poststable and pick whatever you like. Ordering by the value of a custom field is possible too: just usecfvalue:fieldhere and replacefieldwith the real key of the custom field that should be used during the sorting algorithm. order- Either use
ASCorDESCfor 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
descriptionyou 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
fieldthat may be attached multiple times to a single post or page you can split the results by turning on this attribute, e.g. withsplit=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
blahwith its value set to123you would usehaving=blah,123in 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 ↓
My custom field is author and I use it to store the author of the article.
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_pagesalong with the parameterauthors.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
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
now I can order by post_title (i.e.) but I would order by a value of a particular custom field too.
tnx
this is on my todo list and I’ll add it to the plugin in a future version.
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.
search for the first
foreachin the plugin’s file. Inside this loop you’ll have to add code like the following: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
havingattribute now.Thanks!
I’ve added another attribute: use e.g.
limit=5to do just that.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.
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.
# 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);
#}
thank you for the feedback. I’m looking forward to fix the 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
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
I may include another attribute
categorythat 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.
having=blah,”12 34″ does not appear to work.
please enclose the whole value of the attribute
havingin quotes.Is there a way to maintain the permalink in the title and still prefix with <h3>? Thanks.
unfortunately I haven’t documented this but here’s how you’d do it. Add the following to the shortcode:
The special string
%PERMALINK%will be replaced with the corresponding permalink.Leave a Comment