acf repeater field
-
Hi,
I’m using a repeater field in advance custom fields for the taxonomy field to be selected from a dropdown option in a form.
The problem is trying to display the image assocaiated to the taxonomy in the front end, currently when I use the following code the first 10 images are displayed, I want to display only the image selected for the taxonomy;
$args = array(
‘post_type’ => ‘attachment’,
‘post_status’ => ‘inherit’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘media_category’,
‘field’ => ‘id’,
‘terms’ => $term_id
)
)
);https://www.ads-software.com/plugins/enhanced-media-library/
-
Hi,
Are you sure that you use correct term_id? What is the rest of your code?
Nadia
Hi,
Thanks for the speedy response, I’ve been stuck on this bit for days!
I think so…. here is the full code;<?php
$term_id = get_field(‘live_sports_event’);
$term_id = get_sub_field(‘live_sports_event_logo_1’);$args = array(
‘post_type’ => ‘attachment’,
‘post_status’ => ‘inherit’,
‘posts_per_page’ => 1,
‘meta_query’ => array(
array(
‘taxonomy’ => ‘media_category’,
‘field’ => ‘id’,
‘term’ => $term_id
)
)
);
$the_query = new WP_Query( $args );while ( $the_query->have_posts() )
{
$the_query->the_post();
echo wp_get_attachment_image( get_the_ID(), ‘thumbnail’);
}
wp_reset_query();
?>Your second snippet is incorrect. Should be tax_query instead of meta_query
$args = array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'posts_per_page' => 1, 'tax_query' => array( array( 'taxonomy' => 'media_category', 'field' => 'id', 'term' => $term_id ) ) );
unfortunately tax_query doesn’t display any results, which is why I was using meta_query.
It worked when the advance custom field was not a repeater field, but for some reason when I use the same code in a acf repeater field nothing comes up.
You can’t replace a parameter with another one just because it seems to be not working. These two have absolutely different meanings and usage. See https://codex.www.ads-software.com/Class_Reference/WP_Query
I am sure you pass to it incorrect term_id. It’s very easy to check, just use it explicitly like
'terms' => 45
(btw, terms, not term)
If it works you have to check value of your $term_id variable.
Thanks, your quite right by adding the term id explicitly it displays the image.
But the variable for my $term_id which is the name of the taxonomy field in advance custom fields returns a boolean (false), can you shed any light on this at all? Or advise what I’m doing wrong? Thanks again
According to https://www.advancedcustomfields.com/resources/field-types/repeater/ it should be something like this
$rows = get_field('live_sports_event'); $first_row = $rows[0]; $term_id = $first_row['logo' ];
Please check your custom field and sub field names carefully. Or just try
$rows = get_field('live_sports_event'); print_r($rows);
to see where your term_id is.
Hi,
You’ve been a great help! But I still can’t get it to work, I’ve checked my custom field names and sub field names over several times and did a print_r statement, which shows the following;
Array ( [0] => Array (
[choose_live_sports_events] => Array ( [0] => football )
[sports_events_date_time] => 4/27/14 3:34 pm
[live_sports_event_logo_1] => Array ( [0] => stdClass Object ( [term_id] => 656
[name] => Arsenal [slug] => arsenal [term_group] => 0 [term_taxonomy_id] => 732
[taxonomy] => media_category [description] => [parent] => 639 [count] => 1
[filter] => raw
)
)
[live_sports_event_logo_2] => Array ( [0] => stdClass Object ( [term_id] => 673
[name] => Manchester United [slug] => manchester-united [term_group] => 0
[term_taxonomy_id] => 749 [taxonomy] => media_category [description] =>
[parent] => 639 [count] => 1 [filter] => raw
)
)
)
)Could it be because I have a conditional statement in my custom field ? Do I have to add an extra parameter somewhere? also I notice its not the first row that I need to display the image for its actually the rows starting for : live_sports_event_logo_1 & live_sports_event_logo_2
Here is what I have so far:
$rows = get_field(‘live_sports_event’);
$first_row = $rows[0];
$term_id = $first_row[‘live_sports_event_logo_1’];$rows = get_field('live_sports_event'); $first_row = $rows[0]; $term_id_1 = $first_row['live_sports_event_logo_1'][0]->term_id; $term_id_2 = $first_row['live_sports_event_logo_2'][0]->term_id;
and
$args = array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'posts_per_page' => 1, 'tax_query' => array( array( 'taxonomy' => 'media_category', 'field' => 'id', 'terms' => array($term_id_1,$term_id_2) ) ) );
Hi,
Thanks a million!!! I am finally able to display the sports logos from the selected taxonomy using acf.
Great plugin and support highly rated!!
HI,
I’m getting an error message:
Fatal error: Cannot use object of type stdClass as array in /var/www/vhosts/default-domain.com/httpdocs/wp-content/themes/atlas-child/content-single-listing.php on line 341
I have changed the custom fields to allow the user to select different types of sports events.
I need to display the logos for media category selected based on the users selection (which can be more then one), but getting the above stdClass error instead.My array is now:
array(4) {
[0]=> array(23)
{
[“select_live_sports_event”]=> string(8) “football”
[“date_&_time_”]=> string(15) “5/18/14 2:00 pm” [“select_football_teams_playing”]=> object(stdClass)#3522 (10)
{
[“term_id”]=> int(667)
[“name”]=> string(7) “Everton”
[“slug”]=> string(7) “everton”
[“term_group”]=> int(0)
[“term_taxonomy_id”]=> int(743)
[“taxonomy”]=> string(14) “media_category”
[“description”]=> string(0) “”
[“parent”]=> int(639) [“count”]=> int(1)
[“filter”]=> string(3) “raw” }
[“select_football_team_playing_2”]=> object(stdClass)#3518 (10)
{
[“term_id”]=> int(670)
[“name”]=> string(9) “Liverpool”
[“slug”]=> string(9) “liverpool”
[“term_group”]=> int(0)
[“term_taxonomy_id”]=> int(746)
[“taxonomy”]=> string(14) “media_category”
[“description”]=> string(0) “”
[“parent”]=> int(639)
[“count”]=> int(1)
[“filter”]=> string(3) “raw”
}
[“basketball_team1”]=> string(0) “”
[“basketball_team2”]=> string(0) “” [“american_college_conference_team_1”]=> string(0) “” [“american_college_conference_team_2”]=> string(0) “” [“ice_hockey_team_1”]=> string(0) “”
[“ice_hockey_team_2”]=> string(0) “”
[“baseball_team_1”]=> string(0) “”
[“baseball_team_2”]=> string(0) “”
[“formula_1_team”]=> string(0) “”
[“formula_1_team_2”]=> string(0) “”
[“gaa_team_1”]=> string(0) “”
[“gaa_team_2”]=> string(0) “”
[“golf_tournament_team_1”]=> string(0) “”
[“golf_tournament_team_2”]=> string(0) “”
[“rugby_league_team_1”]=> string(0) “”
[“rugby_team_2”]=> string(0) “”
[“world_cup_game_team_1”]=> string(0) “”
[“world_cup_game_team_2”]=> string(0) “”
[“olympics_game”]=> string(0) “”
}
[1]=> array(23)
{
[“select_live_sports_event”]=> string(11) “formula one” [“date_&_time_”]=> string(15) “5/30/14 1:00 pm” [“select_football_teams_playing”]=> string(0) “” [“select_football_team_playing_2”]=> string(0) “” [“basketball_team1”]=> string(0) “”
[“basketball_team2”]=> string(0) “” [“american_college_conference_team_1”]=> string(0) “” [“american_college_conference_team_2”]=> string(0) “” [“ice_hockey_team_1”]=> string(0) “”
[“ice_hockey_team_2”]=> string(0) “”
[“baseball_team_1”]=> string(0) “”
[“baseball_team_2”]=> string(0) “”
[“formula_1_team”]=> object(stdClass)#3521 (10)
{
[“term_id”]=> int(984)
[“name”]=> string(23) “Australia F1 Grand Prix”
[“slug”]=> string(23) “australia-f1-grand-prix”
[“term_group”]=> int(0)
[“term_taxonomy_id”]=> int(1066)
[“taxonomy”]=> string(16) “formula_one_logo”
[“description”]=> string(0) “”
[“parent”]=> int(0)
[“count”]=> int(1)
[“filter”]=> string(3) “raw” }
[“formula_1_team_2”]=> object(stdClass)#3519 (10)
{
[“term_id”]=> int(986)
[“name”]=> string(10) “Bahrain F1”
[“slug”]=> string(10) “bahrain-f1”
[“term_group”]=> int(0)
[“term_taxonomy_id”]=> int(1068)
[“taxonomy”]=> string(16) “formula_one_logo”
[“description”]=> string(0) “”
[“parent”]=> int(0) [“count”]=> int(1)
[“filter”]=> string(3) “raw”
}
[“gaa_team_1”]=> string(0) “”
[“gaa_team_2”]=> string(0) “”
[“golf_tournament_team_1”]=> string(0) “”
[“golf_tournament_team_2”]=> string(0) “”
[“rugby_league_team_1”]=> string(0) “”
[“rugby_team_2”]=> string(0) “”
[“world_cup_game_team_1”]=> string(0) “”
[“world_cup_game_team_2”]=> string(0) “”
[“olympics_game”]=> string(0) “” } [2]=> array(23)
{
[“select_live_sports_event”]=> string(8) “olympics”
[“date_&_time_”]=> string(16) “5/27/16 10:00 am” [“select_football_teams_playing”]=> string(0) “” [“select_football_team_playing_2”]=> string(0) “” [“basketball_team1”]=> string(0) “”
[“basketball_team2”]=> string(0) “” [“american_college_conference_team_1”]=> string(0) “” [“american_college_conference_team_2”]=> string(0) “” [“ice_hockey_team_1”]=> string(0) “”
[“ice_hockey_team_2”]=> string(0) “”
[“baseball_team_1”]=> string(0) “”
[“baseball_team_2”]=> string(0) “”
[“formula_1_team”]=> string(0) “”
[“formula_1_team_2”]=> string(0) “”
[“gaa_team_1”]=> string(0) “”
[“gaa_team_2”]=> string(0) “”
[“golf_tournament_team_1”]=> string(0) “”
[“golf_tournament_team_2”]=> string(0) “”
[“rugby_league_team_1”]=> string(0) “”
[“rugby_team_2”]=> string(0) “”
[“world_cup_game_team_1”]=> string(0) “”
[“world_cup_game_team_2”]=> string(0) “”
[“olympics_game”]=> string(3) “931” } [3]=> array(23)
{
[“select_live_sports_event”]=> string(19) “american conference” [“date_&_time_”]=> string(17) “10/23/14 12:00 am” [“select_football_teams_playing”]=> string(0) “” [“select_football_team_playing_2”]=> string(0) “” [“basketball_team1”]=> string(0) “”
[“basketball_team2”]=> string(0) “” [“american_college_conference_team_1”]=> object(stdClass)#3515 (10)
{
[“term_id”]=> int(935)
[“name”]=> string(25) “Atlantic Coast Conference”
[“slug”]=> string(25) “atlantic-coast-conference”
[“term_group”]=> int(0)
[“term_taxonomy_id”]=> int(1015)
[“taxonomy”]=> string(16) “american_college”
[“description”]=> string(0) “”
[“parent”]=> int(0)
[“count”]=> int(1)
[“filter”]=> string(3) “raw”
}
[“american_college_conference_team_2”]=> object(stdClass)#3513 (10) { [“term_id”]=> int(937)
[“name”]=> string(18) “Big Sky Conference”
[“slug”]=> string(18) “big-sky-conference”
[“term_group”]=> int(0)
[“term_taxonomy_id”]=> int(1017)
[“taxonomy”]=> string(16) “american_college”
[“description”]=> string(0) “”
[“parent”]=> int(0)
[“count”]=> int(1)
[“filter”]=> string(3) “raw” }
[“ice_hockey_team_1”]=> string(0) “”
[“ice_hockey_team_2”]=> string(0) “”
[“baseball_team_1”]=> string(0) “”
[“baseball_team_2”]=> string(0) “”
[“formula_1_team”]=> string(0) “”
[“formula_1_team_2”]=> string(0) “”
[“gaa_team_1”]=> string(0) “”
[“gaa_team_2”]=> string(0) “”
[“golf_tournament_team_1”]=> string(0) “”
[“golf_tournament_team_2”]=> string(0) “”
[“rugby_league_team_1”]=> string(0) “”
[“rugby_team_2”]=> string(0) “”
[“world_cup_game_team_1”]=> string(0) “”
[“world_cup_game_team_2”]=> string(0) “”
[“olympics_game”]=> string(0) “”
}
}Here is what I have set up:
<?php
$rows = get_field(‘live_bar_sports_event’);
$first_row = $rows[0];
$term_id_1 = $first_row[‘select_football_teams_playing’][0]->term_id;
$args = array(
‘post_type’ => ‘attachment’,
‘post_status’ => ‘inherit’,
‘posts_per_page’ => 1,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘media_category’,
‘field’ => ‘id’,
‘terms’ => array($term_id_1)
)
)
);$the_query = new WP_Query( $args );
while ( $the_query->have_posts() )
{
$the_query->the_post();
echo wp_get_attachment_image( get_the_ID(), ‘thumbnail’);
}
wp_reset_query();?> vs
<?php$rows = get_field(‘live_bar_sports_event’);
$first_row = $rows[0];
$term_id_2 = $first_row[‘select_football_team_playing_2’][0]->term_id;
$args = array(
‘post_type’ => ‘attachment’,
‘post_status’ => ‘inherit’,
‘posts_per_page’ => 1,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘media_category’,
‘field’ => ‘id’,
‘terms’ => array($term_id_2)
)
)
);$the_query = new WP_Query( $args );
while ( $the_query->have_posts() )
{
$the_query->the_post();
echo wp_get_attachment_image( get_the_ID(), ‘thumbnail’);
}
wp_reset_query(); ?>This is repeated for each media Category :
<?php$rows = get_field(‘live_bar_sports_event’);
$first_row = $rows[0];
$term_id_1 = $first_row[‘basketball_team1’][0]->term_id;
$args = array(
‘post_type’ => ‘attachment’,
‘post_status’ => ‘inherit’,
‘posts_per_page’ => 1,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘baseball_logo’,
‘field’ => ‘id’,
‘terms’ => array($term_id_1)
)
)
);$the_query = new WP_Query( $args );
while ( $the_query->have_posts() )
{
$the_query->the_post();
echo wp_get_attachment_image( get_the_ID(), ‘thumbnail’);
}
wp_reset_query();?> vs
<?php
$rows = get_field(‘live_bar_sports_event’);
$first_row = $rows[0];
$term_id_2 = $first_row[‘basketball_team2’][0]->term_id;
$args = array(
‘post_type’ => ‘attachment’,
‘post_status’ => ‘inherit’,
‘posts_per_page’ => 1,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘baseball_logo’,
‘field’ => ‘id’,
‘terms’ => array($term_id_2)
)
)
);$the_query = new WP_Query( $args );
while ( $the_query->have_posts() )
{
$the_query->the_post();
echo wp_get_attachment_image( get_the_ID(), ‘thumbnail’);
}
wp_reset_query();?>Hello @bm-rinths,
If the problem is still actual for you please post your code using ‘code’ button at the top of the comment editor. It’s very hard to read it. Also, what you need is actually a basic PHP and WordPress assistance, not EML support question. Please make sure you checked their documentation first. Thanks.
Nadia
- The topic ‘acf repeater field’ is closed to new replies.