I'm trying to display 4 random instances of a custom field from random posts without repeating the custom field. In other words, it's okay to get several custom field instances from one post, but not okay to get the same custom field twice instance.
So I started with one query that produced 4 unique random instances of the custom field. See this StackOverflow Question
This worked fine except for the fact that all 4 instances were from the same post, so I tried 4 queries storing the value of the custom field in an array and tried checking each query against the array, but it doesn't seem to be working. I must be missing something. Here is my code showing all four queries:
<?php $args = array(
'orderby' => 'rand',
'ignore_custom_sort' => TRUE,
'post_type' => 'bios',
'tax_query' => array(
array(
'taxonomy' => 'bio_type',
'field' => 'slug',
'terms' => 'leadership-team',
),
),
'posts_per_page' => 1,
'meta_key' => '_simple_fields_fieldGroupID_82_fieldID_1_numInSet_0',
'meta_value' => '', // check that meta_key is not empty
'meta_compare' => '!='
); ?>
<?php
// The Query to show a specific Custom Field
$the_query = new WP_Query($args);
$ids = array();
?>
<?php
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<?php
// The Loop
echo '<div class="row vertical-pad-small">';
echo '<div class="columns large-8">';
$acc = simple_fields_fieldgroup('bio_accordion');
$keys = array_rand($acc);
$title = $acc[$keys]['title'];
echo '<div>';
echo '<a>'.$title.'</a>';
echo '<span>';
echo $acc[$keys]['content'];
echo '</span>';
echo '</div>';
echo '</div>';
echo '</div>';
endwhile;
// Reset Post Data
wp_reset_postdata();
$ids[] = $title;
?>
<pre>
<?php var_dump($ids); ?>
</pre>
<?php
$args2 = array(
'orderby' => 'rand',
'ignore_custom_sort' => TRUE,
'post_type' => 'bios',
'tax_query' => array(
array(
'taxonomy' => 'bio_type',
'field' => 'slug',
'terms' => 'leadership-team',
),
),
'posts_per_page' => 1,
'meta_key' => '_simple_fields_fieldGroupID_82_fieldID_1_numInSet_0',
'meta_value' => '',$ids, // check that meta_key is not empty and is not in $ids
'meta_compare' => '!='
);
?>
<?php $the_query2 = new WP_Query($args2);
while ( $the_query2->have_posts() ) : $the_query2->the_post();
?>
<?php
$bio_fields = simple_fields_fieldgroup('bio');
$adult_image = $bio_fields['adult_picture']['image']['medium_square'];
$title = $bio_fields['title'];
$name = get_the_title();
?>
<?php
// The Loop
echo '<div class="row vertical-pad-small">';
echo '<div class="columns large-8">';
$acc = simple_fields_fieldgroup('bio_accordion');
$keys = array_rand($acc);
$title = $acc[$keys]['title'];
echo '<div>';
echo '<a>'.$title.'</a>';
echo '<span>';
echo $acc[$keys]['content'];
echo '</span>';
echo '</div>';
echo '</div>';
echo '</div>';
endwhile;
// Reset Post Data
wp_reset_postdata();
$ids[] = $title;
?>
<?php
$args3 = array(
'orderby' => 'rand',
'ignore_custom_sort' => TRUE,
'post_type' => 'bios',
'tax_query' => array(
array(
'taxonomy' => 'bio_type',
'field' => 'slug',
'terms' => 'leadership-team',
),
),
'posts_per_page' => 1,
'meta_key' => '_simple_fields_fieldGroupID_82_fieldID_1_numInSet_0',
'meta_value' => '',$ids,// check that meta_key is not empty and is not in $ids
'meta_compare' => '!='
);
?>
<?php $the_query3 = new WP_Query($args3);
while ( $the_query3->have_posts() ) : $the_query3->the_post();
?>
<?php
// The Loop
echo '<div class="row vertical-pad-small">';
echo '<div class="columns large-8">';
$acc = simple_fields_fieldgroup('bio_accordion');
$keys = array_rand($acc);
$title = $acc[$keys]['title'];
echo '<div>';
echo '<a>'.$title.'</a>';
echo '<span>';
echo $acc[$keys]['content'];
echo '</span>';
echo '</div>';
echo '</div>';
echo '</div>';
endwhile;
// Reset Post Data
wp_reset_postdata();
$ids[] = $title;
?>
<?php
$args4 = array(
'orderby' => 'rand',
'ignore_custom_sort' => TRUE,
'post_type' => 'bios',
'tax_query' => array(
array(
'taxonomy' => 'bio_type',
'field' => 'slug',
'terms' => 'leadership-team',
),
),
'posts_per_page' => 1,
'meta_key' => '_simple_fields_fieldGroupID_82_fieldID_1_numInSet_0',
'meta_value' => '',$ids, // check that meta_key is not empty and is not in $ids
'meta_compare' => '!='
);
?>
<?php $the_query4 = new WP_Query($args4);
while ( $the_query4->have_posts() ) : $the_query4 ->the_post();
?>
<?php
// The Loop
echo '<div class="row vertical-pad-small">';
echo '<div class="columns large-8">';
$acc = simple_fields_fieldgroup('bio_accordion');
$keys = array_rand($acc);
$title = $acc[$keys]['title'];
echo '<div>';
echo '<a>'.$title.'</a>';
echo '<span>';
echo $acc[$keys]['content'];
echo '</span>';
echo '</div>';
echo '</div>';
echo '</div>';
endwhile;
// Reset Post Data
wp_reset_postdata();
$ids[] = $title;
?>
Can anyone help me?
'meta_value' => '',$ids,
'meta_value'=>$ids,
? If I do that I get this error: Warning: trim() expects parameter 1 to be string, array givenimplode(',', $ids)
. Sorry not a wordpress developer so I don't really know what this does other than that's an obvious error.