Home › Forums › Add-ons › Repeater Field › Echo Repeater-Field alphabetically query_posts
Hi
We run an Eventsite for a Concert-Location. We do that with a Custom-Post-Type “events” and the date-function from ACF. That works fine.
We write a new Post (Event) and in there whe have a repeater-field “artists” with the field “artistname” playing on this event.
Now we want a new page with a list of all Artists (artistname) played at our club in the past.
So the query should find
1.past events
2. with the repeater-field “artists” and give a list of artistnames.
For Example
Event one with Bandname A, Bandname Z
Event two with Bandname B and Bandname X
On the new Site we want a list like
Bandname A
Bandname B
Bandname X
Bandname Z
My query_post so far
<?php
$today = date('Ymd');
$args = array(
'post_type' => 'events',
'meta_key' => 'date',
'meta_value' => $today,
'meta_compare' => '<',
'posts_per_page' => '50',
'meta_query' => array(
array(
'key' => 'artists',
'value' => 0,
'type' => 'NUMERIC',
'compare' => '>',
),
array(
'meta_key' => 'artistname',
'orderby' => 'meta_value',
'order' => 'ASC'
)
)
);
query_posts( $args );
?>
Any help?
Hi @rhodesign
If you need to query the subfield, please take a look at this page (under “4. Sub custom field values” section): http://www.advancedcustomfields.com/resources/query-posts-custom-fields/.
I hope this helps.
Thank you for your hint. I tried alot but have no clue where it stucks. would be nice if someone can give me another hint.
My repeater-field is “artists“, in there is a sub-field called “artistname“.
My goal is a list of artistnames played in the past, sorted alphabetically (if possible without doubles, if an artist player more than once in the club).
So far it displays all artistnames, but not alphabetically.
My query so far:
<?php
// filter
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'artists_%", "meta_key LIKE 'artists_%", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
// find todays date
$today = date('Ymd');
$args = array(
'posts_per_page' => '-1',
'post_type' => 'veranstaltungen',
'orderby' => 'artist_clause',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
'date_clause' => array(
'key' => 'datum',
'value' => $today,
'compare' => '<',
),
'artist_clause' => array(
'key' => 'artists_%_artistname',
)
)
);
query_posts( $args );
?>
Thank you
ok, now I got it.
This is my solution if someone is interested:
<?php
// filter
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'artists_%", "meta_key LIKE 'artists_%", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
// find todays date
$today = date('Ymd');
$args = array(
'posts_per_page' => '-1',
'post_type' => 'veranstaltungen',
'orderby' => 'meta_value',
'meta_key' => 'artists_%_artistname',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'datum',
'value' => $today,
'compare' => '<',
),
array(
'key' => 'artists_%_artistname',
)
)
);
query_posts( $args );
?>
ohh, not absolutely solved, sorry.
With the code above I get the list of all sub custom field values “artistname”, but not 100% correct. They are still sorted by posts (query-type “events”) somehow.
If events have only one row it works fine
exmpl:
Event 1 – artist – Abba
Event 2 – artist – Duran Duran
Event 3 – artist – Foo Fighters
But if an events has more then one row, not the row itselves is posted alphabeticaly.
exmpl:
Event 4 – artist – Coldplay, Aerosmith, Eminem
The list looks like this:
It should look lite that, 100% alphabeticaly:
Hi @rhodesign
If you want to sort a repeater, please take a look at this page: http://www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/.
Hope this helps.
Hi James
No, sorry, I don`t get it to work. I always get errors.
My repeater is: “artists”
Sub-fields: “artistname” / “artistweb”
“artistname” is always set, “artistweb” only if an artsit has a website.
I want a list sorted by “artistname”
Would be very happy if someone can help me out.
Roman
Hi Roman,
Could you please share the JSON export of your field group?
Thanks!
Hi James
Thak you!
This is my JSON-File.
“field_56c19dd4c3e9c” is the repeater I want the alphabetical list of.
{
"key": "group_56cd64f9096fa",
"title": "Veranstaltungen",
"fields": [
{
"key": "field_54c7409720a78",
"label": "Date",
"name": "date",
"type": "date_picker",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"display_format": "l, d. F Y",
"return_format": "Ymd",
"first_day": 1
},
{
"key": "field_54c7412220a79",
"label": "Time",
"name": "time",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
},
{
"key": "field_54c7a407e1264",
"label": "Event-Type",
"name": "type",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
},
{
"key": "field_54c7a45be1265",
"label": "Entry fee",
"name": "entry",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
},
{
"key": "field_54d9d07b10c0a",
"label": "Supporting Act",
"name": "support",
"type": "text",
"instructions": "Supporting-Act",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
},
{
"key": "field_54c8992084720",
"label": "Links",
"name": "links",
"type": "repeater",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"row_min": "",
"row_limit": "",
"layout": "table",
"button_label": "Add new link",
"min": 0,
"max": 0,
"collapsed": "",
"sub_fields": [
{
"key": "field_54c8992e84721",
"label": "Linkname",
"name": "linkname",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"formatting": "html",
"maxlength": "",
"readonly": 0,
"disabled": 0
},
{
"key": "field_54c8993c84722",
"label": "Linkadresse",
"name": "linkadresse",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"formatting": "html",
"maxlength": "",
"readonly": 0,
"disabled": 0
}
]
},
{
"key": "field_56c19dd4c3e9c",
"label": "Artists",
"name": "artists",
"type": "repeater",
"instructions": "This field is used for the Artist-List.",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"row_min": "",
"row_limit": "",
"layout": "table",
"button_label": "Add Artist",
"min": 0,
"max": 0,
"collapsed": "",
"sub_fields": [
{
"key": "field_56c19de1c3e9d",
"label": "Artist Name",
"name": "artistname",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"formatting": "html",
"maxlength": "",
"readonly": 0,
"disabled": 0
},
{
"key": "field_56c1abe6bf667",
"label": "Artist-Website",
"name": "artistweb",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"formatting": "html",
"maxlength": "",
"readonly": 0,
"disabled": 0
}
]
},
{
"key": "field_54e5d86e27b60",
"label": "Presales",
"name": "presales",
"type": "text",
"instructions": "Link zur Vorverkaufsseite \"http:\/\/www....\"",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": "",
"readonly": 0,
"disabled": 0
},
{
"key": "field_54c8b1bf11fde",
"label": "Image Gallery",
"name": "gallery",
"type": "repeater",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"collapsed": "",
"min": "",
"max": "",
"layout": "table",
"button_label": "Fotos hinzuf\u00fcgen",
"sub_fields": [
{
"key": "field_54c8b1dc11fdf",
"label": "Bild",
"name": "bild",
"type": "image",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"preview_size": "thumbnail",
"library": "all",
"return_format": "id",
"min_width": 0,
"min_height": 0,
"min_size": 0,
"max_width": 0,
"max_height": 0,
"max_size": 0,
"mime_types": ""
}
]
},
{
"key": "field_54cb98c819900",
"label": "R\u00e9sum\u00e9",
"name": "resume",
"type": "wysiwyg",
"instructions": "Das ist ein abschliessender Text zum Event. Dieser wird angezeigt sobald Fotos in der Fotogalerie sind.",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"tabs": "all",
"toolbar": "full",
"media_upload": 1
}
],
"location": [
[
{
"param": "post_type",
"operator": "==",
"value": "post"
}
]
],
"menu_order": 5,
"position": "normal",
"style": "seamless",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": 1,
"description": "",
"modified": 1456303465
}
Hi @rhodesign
This is what I can come up with:
// find todays date
$today = date('Ymd');
$args = array(
'numberposts' => -1,
'post_type' => 'test',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'date',
'value' => $today,
'compare' => '<',
),
)
);
$the_query = new WP_Query( $args );
$post_ids = wp_list_pluck( $the_query->posts, 'ID' );
$artist_list = array();
foreach($post_ids as $post_id){
$artist_repeater = get_field('artists', $post_id);
foreach($artist_repeater as $artist_detail){
array_push($artist_list, $artist_detail);
}
}
$artist_list = array_map("unserialize", array_unique(array_map("serialize", $artist_list)));
$order = array();
foreach( $artist_list as $i => $row ) {
$order[ $i ] = $row['artistname'];
}
array_multisort( $order, SORT_ASC, $artist_list );
echo "<pre>";
print_r($artist_list);
echo "</pre>";
This code will list the artists name and websites in an array. It’s more of a PHP issue, so If you don’t understand it, please ask in the PHP forum or hire a developer to help you out with it.
I hope this helps.
The topic ‘Echo Repeater-Field alphabetically query_posts’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.