How do I grab these custom fields
Great question
In general, you have to write a query which gets all the posts. reference
Then from those posts, you extract the
custom fields
In other words, to make it happen dynamically as things are always changing, you have to be able to write a “mathematical” formula to specify which posts and which custom fields are to be added.
Can you tell me in words which posts? All of them? Maybe just the ones with a certain category or tag? Maybe there’s a list somewhere with checkboxes and the user checks them off?
Thread Starter
Josh
(@modernspark)
Santeven,
Thanks for the response! It’s actually a specific custom post type. So in the example I mentioned, it would be the custom post type “items”. I’d like to add the custom field “prices” of multiple “item” posts on a dashboard-like page.
I understand queries and custom fields pretty well, I just don’t know how to do the mathematical addition on a separate page.
If you have any possible examples I could try, I’d appreciate it!
Super simple example:
<?php $args = array (
'post_type' => 'item', // Get your 'item' post type
'posts_per_page' => -1 // -1 means 'as many of this post type as there are'
);
$postnum = 0;
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts()) :
$postnum = $postnum + $customfieldvalue; // add the custom field value below to the $postnum variable
$the_query->the_post();
$customfieldvalue = get_post_meta($post->ID, 'number', true); // this is getting the value for a custom field called 'number'
echo $customfieldvalue; // this is just echoing it for example, on a per post basis - so every individual post's value for 'number'
endwhile; }
echo $postnum; // and then this displays the total amount ?>
Thread Starter
Josh
(@modernspark)
@clicknathan —
This is really close. It’s not adding the final number though. Any ideas? For example:
price = 5
price = 10
price = 15
result = 15 instead of 30
Thread Starter
Josh
(@modernspark)
Also note – when I say final number, it’s actually the oldest post.
I tested it yesterday before posting Josh and again just this morning to make sure I didn’t change anything vital when adding the comments in.
Can you post your code that you’re using?
Also, you may want to be sure that every ‘item’ has a corresponding custom field value in case not having one tweaks out the math somehow.
Thread Starter
Josh
(@modernspark)
It’s the same code you posted, I just changed the name of the custom field and added a taxonomy to the array. For some reason it adds everything correctly but omits the oldest post. Here’s a real example of what it outputs:
80
65
69
88
Total = 214
(It should be 302, but it omits the final number 88)
USING THIS CODE:
<?php $args = array (
'post_type' => 'item', // Get your 'item' post type
'my-taxonomy' => 'taxonomy-one',
'posts_per_page' => -1 // -1 means 'as many of this post type as there are'
);
$postnum = 0;
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts()) :
$postnum = $postnum + $customfieldvalue; // add the custom field value below to the $postnum variable
$the_query->the_post();
$customfieldvalue = get_post_meta($post->ID, 'clicks_this_month', true); // this is getting the value for a custom field called 'number'
echo $customfieldvalue; echo '<br />'; // this is just echoing it for example, on a per post basis - so every individual post's value for 'number'
endwhile; } ?>
<br />
<?php echo $postnum; // and then this displays the total amount ?>
Instead of:
'my-taxonomy' => 'taxonomy-one',
Shouldn’t it be:
'taxonomy' => 'taxonomy-one',
?
Thread Starter
Josh
(@modernspark)
Santeven, I don’t think so. Either way, It didn’t work even without the taxonomy added to the array.
my-taxonomy is wrong, it should be taxonomy as per Santeven’s suggestion, but also it should be in a tax_query array. See here: https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
However, that’s irrelevant. No clue why it isn’t picking up your last post. I’ve tried it with 3, 4 and 10 posts and it always works for me.
Thread Starter
Josh
(@modernspark)
Thanks for the taxonomy note. I updated the code, and it’s still not picking up the last post. Again, I appreciate all your help. It’s so close to working, so if anyone has any other ideas why this might not be working, i’d love some more insight. Thanks!
Here’s my actual code now (taxonomy/slugs are different from previous examples):
<?php $args = array (
'post_type' => 'ad-campaigns',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'advertiser-page',
'field' => 'slug',
'terms' => 'my-advertiser',
),
),
);
$postnum = 0;
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts()) :
$postnum = $postnum + $customfieldvalue; // add the custom field value below to the $postnum variable
$the_query->the_post();
$customfieldvalue = get_post_meta($post->ID, 'ad_clicks', true); // this is getting the value for a custom field
echo $customfieldvalue; echo '<br />'; // this is just echoing it for example, on a per post basis - so every individual post's value
endwhile; } ?>
<br />
<?php echo $postnum; // and then this displays the total amount ?>