Home › Forums › Add-ons › Repeater Field › returning values from repeater fields filtered by another custom field › Reply To: returning values from repeater fields filtered by another custom field
seems i have to do this using $wpdb …
//average for all of post_type by assessor
function q_average_assessor( $fieldname, $posttype, $assessor) {
global $wpdb;
$repeaterfieldname = 'values_and_outcomes_%_' . $fieldname;
// get all rows from the postmeta table where post_type = $postype AND has a repeater subfield of $fieldname
// - http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results
$rows = $wpdb->get_results($wpdb->prepare(
"
SELECT *
FROM wp_postmeta
INNER JOIN wp_posts ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE wp_posts.post_type=%s
AND wp_postmeta.meta_key LIKE %s
",
$posttype,
$repeaterfieldname
));
$coreval = 0;
$counter = 0;
// loop through the results
if( $rows )
{
foreach( $rows as $row )
{
// for each result, find the 'repeater row number' and use it to load the image sub field!
preg_match('_([0-9]+)_', $row->meta_key, $matches);
$meta_key = 'values_and_outcomes_' . $matches[0] . '_' . $fieldname; // $matches[0] contains the row number!
// use get_post_meta
$thisassessor = get_post_meta( $row->post_id, 'assessor', true );
if($thisassessor == $assessor) {
$counter++;
$coreval = $coreval + get_post_meta( $row->post_id, $meta_key, true );
}
}
}
$paverage = $coreval / $counter;
$average = round($paverage, 1);
return $average;
}
but means a lot of my previous code is wrong. assumption is that for a query on an archive page (ie involving more than one post and repeater fields) get_posts() doesn’t work but $wpdb does?
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.