Okay so I'm using Magic Fields 2 on an old WordPress project (rather than ACF unfortunately) and I've got some confusion happening while I'm writing PHP.
Basically I'm on a page template that calls the Magic Fields group from it's post ID because the fields themselves are on a custom post type. They're just being displayed on a page template.
Then I'm looping through that group's fields to get an ID value that is just choosing another post with the Magic Field's Related Type field.
Now here's my problem after using var_dump to debug - instead of giving me the ID value for EACH post chosen with the related type (there's currently two) - it's repeating the first ID twice.
This to me is a PHP question versus Magic Fields. So how do I get EACH ID value within the existing foreach without resorting to another foreach? I need to grab each ID and then use those IDs in continuing PHP after. Here is what I have:
$chall_entries = get_group('challenge_entry', $post_id = 4643);
foreach($chall_entries as $art_entry): //artisan challenges only
if( $art_entry['challenge_entry_challenge_type'][1] == 'Artisan' ):
global $task_post_id;
$task_post_id = intval($art_entry['challenge_entry_assign_task'][1]);
echo $task_post_id; //THIS IS REPEATING THE SAME ID TWICE - WHY?
//HOW DO I GET EACH ID AND ITERATE THEM FROM THE MAGIC FIELDS
//WITHOUT USING YET ANOTHER FOR EACH?
endif;
endforeach;
To give an idea of how the Magic Fields are set up on the dashboard, they're like this:
Custom Post Type w/ MFs
Repeating Group of Fields (there are two groups right now)
Fields within each group
So if there are TWO groups which is how it is now:
Custom Post Type w/ MFs
Group A
Group A Fields
Group B
Group B Fields
So the problem is that Group A's field is repeating versus looping to also get Group B the second time...
CODE UPDATE #1 - 7/6/2015
Just to clarify, the goal is to loop through two separate MF groups. One group has the ID I need for the second group. That ID is a parameter needed to create the second group's loop except I need that ID to be dynamic and not hardcoded, hence the first group's PHP. Here's the code for the "second group" if it helps:
$artisan_chall_task = get_group('task_entry', $post_id=$task_post_id); //SEE THE ID HERE?
foreach($artisan_chall_task as $art_chall_task):
$task_ach_id = intval($art_chall_task['task_entry_achievement_id'][1]); //achievement ID
$task_ach_link = get_the_permalink($task_ach_id); //achievement link
echo "<tr>";
echo "<td class='mobile-first'>" . $art_chall_task['task_entry_task_name'][1] . "</td>";
echo "<td class='mobile-second'>" . $art_chall_task['task_entry_task_desc'][1] . "</td>";
echo "<td class='mobile-third'><div class='" . $art_chall_task['task_entry_task_difficulty'][1] . "-stars margin-auto'></div></td>";
echo "<td class='mobile-fourth'><a href='" . $task_ach_link . "'>" . get_the_post_thumbnail( $task_ach_id, 'thumbnail' ) . "</a></td>";
echo "</tr>";
endforeach;
Problems I'm having so far with WebToure's Tips:
I'm getting 1 from the $length variable with var_dump, but there are two items in that array if I var_dump($art_entry['challenge_entry_assign_task']); This gives me:
array(1) {
[1]=>
string(4) "4644"
}
array(1) {
[1]=>
string(4) "4652"
}
And so the for loop with $length is giving me this error:
Undefined offset: 0
I also need to pass the ID variable from the first group, outside that first group to be used as a parameter to start a second group - if not using global, how else do I do that?
CODE UPDATE #2 - 7/6/2015
I'm using the function solution from WebToure. This is in the template:
foreach($chall_entries as $art_entry): //artisan challenges only
if( $art_entry['challenge_entry_challenge_type'][1] == 'Artisan' ):
$task_post_id = intval($art_entry['challenge_entry_assign_task'][1]);
//I GET MORE THAN ONE ID HERE IF I VAR_DUMP $task_post_id
endif;
endforeach;
//OUT HERE THOUGH VAR_DUMP $task_post_id ONLY GIVES ME FIRST ID?
//I WANT THE SECOND GROUP'S LOOPING HERE
//AND EACH ID GRABBED FROM THE FIRST GROUP WITHOUT BEING IN THAT FOREACH
print_task_entry( $task_post_id );
And this is in functions.php:
function print_task_entry( $post_id ) {
$artisan_chall_task = get_group( 'task_entry', $post_id ); // Passing the $post_id
foreach($artisan_chall_task as $art_chall_task):
$task_ach_id = intval($art_chall_task['task_entry_achievement_id'][1]); //achievement ID
$task_ach_link = get_the_permalink($task_ach_id); //achievement link
echo "<tr>";
echo "<td class='mobile-first'>" . $art_chall_task['task_entry_task_name'][1] . "</td>";
echo "<td class='mobile-second'>" . $art_chall_task['task_entry_task_desc'][1] . "</td>";
echo "<td class='mobile-third'><div class='" . $art_chall_task['task_entry_task_difficulty'][1] . "-stars margin-auto'></div></td>";
echo "<td class='mobile-fourth'><a href='" . $task_ach_link . "'>" . get_the_post_thumbnail( $task_ach_id, 'thumbnail' ) . "</a></td>";
echo "</tr>";
endforeach;
}
The Final Working Solution - 7/7/2015:
Looks like the issue was a mixture of PHP with WebToure's solution was good, and I had a modal plugin interfering. So it appeared as if all the results were printing in one modal and all at once versus each having their own modals. I just needed to ensure that each result had their own modal with a counter class made with PHP. In the end though, this was the final result (without the modal code or counter).
In Template File:
$chall_entries = get_group('challenge_entry', $post_id = 4643);
foreach($chall_entries as $art_entry): //artisan challenges only
if( $art_entry['challenge_entry_challenge_type'][1] == 'Artisan' ):
$task_post_id = intval($art_entry['challenge_entry_assign_task'][1]);
print_task_entry( $task_post_id );
endif;
endforeach;
In Functions.php:
function print_task_entry( $post_id ) {
$artisan_chall_task = get_group( 'task_entry', $post_id ); // Passing the $post_id
foreach($artisan_chall_task as $art_chall_task):
$task_ach_id = intval($art_chall_task['task_entry_achievement_id'][1]); //achievement ID
$task_ach_link = get_the_permalink($task_ach_id); //achievement link
echo "<tr>";
echo "<td class='mobile-first'>" . $art_chall_task['task_entry_task_name'][1] . "</td>";
echo "<td class='mobile-second'>" . $art_chall_task['task_entry_task_desc'][1] . "</td>";
echo "<td class='mobile-third'><div class='" . $art_chall_task['task_entry_task_difficulty'][1] . "-stars margin-auto'></div></td>";
echo "<td class='mobile-fourth'><a href='" . $task_ach_link . "'>" . get_the_post_thumbnail( $task_ach_id, 'thumbnail' ) . "</a></td>";
echo "</tr>";
endforeach;
}
Thanks WebToure! :-)
var_dump
output clearly shows a single element array. You are seeing twoarray(1) {...
becausevar_dump
is called every time in the loop (in this case it gets called twice). So the$art_chall_task['task_entry_achievement_id']
variable is an array with a single element (the ID you are looking for) at index 1.