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, 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 ↓
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.
[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.
# 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.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.
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.
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.
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.
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?
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:
Given that the name of the input field in your sidebar is named
user_fieldthe 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.
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…!
Thank You. Great plugin!
you should make sure that the number of posts with a certain custom field that you’re selecting – with the
fieldattribute – isn’t too large; I guess a few thousand posts should work though. Furthermore you can use thelimitattribute: 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_postsfunction. It allows you to select posts based on their custom fields too;meta_key,meta_valueormeta_comparecome to mind. This function call before the loop and a properly configured pagination may help you as well.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