I looked into adding this option. The [loop] shortcode uses WP_Query to get posts, and there doesn’t seem to be a way to get posts by beginning characters of field value. So, I think the solution is to use a combination of [loop] and [if].
Basically what I wanted to do was have the posts filtered by the loop so that i could use the [if empty] shortcode to see if the result was empty. Maybe there is a way to do this already and I am just not finding it.
After looking at the WP_Query documentation page I have two ideas about how to possibly get the desired result.
First, it appears that you can compare strings to the another string using the following operators.
‘=’, ‘!=’, ‘>’, ‘>=’, ‘<‘, ‘<=’, ‘LIKE’, ‘NOT LIKE’, ‘IN’, ‘NOT IN’, ‘BETWEEN’, ‘NOT BETWEEN’, ‘EXISTS’.
Additionally, a relation can be added so that two operators can work together. I believe the use of an >= operator and a < operator would allow this to work. >=M and <N would give all strings that start with M. You could allow people to select the beginning and end values.
Alternatively, the results of the query could be retrieved and then filtered using a function in Custom Content Shortcode before returning the results.
I see, I was consulting the same section in the codex, thinking of how to query the beginning of a field value. The compare and relation parameters for [loop] will pass the same operators to WP_Query, so you could experiment with them.
Your second suggestion seems more straight-forward, to filter the query results inside CCS. OK, I’ll look at implementing this in the next update.
OK, I added the start parameter to the loop shortcode. As per your second suggestion, it will filter the posts by checking the beginning of field value:
[loop type="product" field="title" start="A"]
For the field, you can use predefined field names or custom field.
I’m just curious, how do you plan to use this?
Thanks very much for adding it.
I am working on a site for a homeowners association and creating a directory of residents. I am using an alphabetical listing by last name and wanted a way to add some text if no residents with a last name that starts with a particular letter exist. By adding the filtering to the loop I can use the [if empty] shortcode to determine if the result is empty.
I see! That makes sense why you needed the first letter of a field, to use for a directory listing. Thanks for sharing, it’s always interesting to hear the different ways the plugin is being used.