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 ↓
ab 3.1 funktioniert dein Plugin leider nicht mehr, hast du da ne Lösung?
VG
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.
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!
es ist eigentlich viel einfacher: die Angabe von
excerptfieldweglassen und das Standard-Exzerpt wird genutzt; Tabellewp_posts, Spaltepost_excerpt. Natürlich kann man dies auch im Code hartkodieren, dann aber bitteget_the_excerptnutzen.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.
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.
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!
this message says that you should add a custom field to the shortcode like so:
This would retrieve all posts with the field
testhaving another field calledblahwhich is set to the value123.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?
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.
@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
splitattribute 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
orderbyandorderto alter the order of the posts.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