I have a query loop for a custom post type that has some custom fields on it.
I want to capture the values of these fields to do some maths on them. A simple stats type setup.
I assume that there isn’t a native way to do this so I’ll need to write a PHP function. But where do I place this? Can I just drop a code element inside the query loop?
How can I make the results available to the website in general and not just within the loop?
The custom PHP function could be in your child theme, or code snippet plugin. Then you’d use Bricks’ echo dynamic tag to use the function to return the value, passing in the values of any custom fields from the loop.
Basic example..
/* always register custom function name */
add_filter( 'bricks/code/echo_function_names', function() {
return [
'my_custom_function',
];
} );
/* custom function for doing some maths */
function my_custom_function($price) {
// (e.g. add 20%)
$calculated_price = $price * 1.2;
return $calculated_price;
}
Then in Bricks, in the loop, you’d use the function, passing in the ACF tags..
Many thanks David, That part works a treat and I can extract data from each post to manipulate. However I wasn’t clear enough in stating my original problem, as usual.
If I have a query loop How do I capture values from each loop entry ( CPT ) and perform calculations on ALL of them?
What I’m trying to do is.
Loop through a load of CPTs
Extract some data from each ( for simplicity assume just extract a single number).
Add all of these numbers together to get a total for the entire query loop.
Display the resulting total outside of the loop. Perhaps in a sidebar or the header.
Currently everything I’ve tried only seems to act on each individual CPT in the loop. So not sure how to save the data from each CPT to pass to a function.
Hope that makes sense?
Basically I’m trying to do get some simple stats about my CPTs.