How to count meta_key(s) of a page? - php

Is it possible to count the amount of the same meta_keys for the same post_id.
I have a page with the post_id 44 and every time a user submits something on this page a new database rule is added with the meta_key "post_rating_0".
There are over 30 rules now for this page and I want to count them.
For other pages the same thing.
Is this possible?
Code so far:
global $post;
$query_args = array(
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => 'post_rating_0'
// 'fields' => 'SUM(amount_to_paid)',
);
$sum = 0;
$query = new WP_Query($query_args);
if ( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
// do the processing for each post
$sum = $sum + intval(get_post_meta(get_the_id(),'post_rating_0', true ));
}
}
echo $sum ;
This is what I got so far... But what this does is echo the value of "2" what is very logic. It searches for posts with the meta_key "post_rating_0", counts them and that are 2 posts.
What I'm trying to do is count the amount of meta_keys "post_rating_0" are in one page.

This modified code will give you an array with all post_id and the corresponding amount of your specific meta:
$meta_arr = array();
$query = new WP_Query($query_args);
if ( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
$post_id = get_the_id();
$meta_arr[$post_id] = intval(get_post_meta($post_id, 'post_rating_0', false));
}
}
print_r($meta_arr);

Did some more research and with some modifications I managed to got it working. This is the final code:
$pagina_id_test = get_the_id();
// retrieve all meta_values with key 'post_rating_0' from database
$reviews_posts = $wpdb->get_results("
SELECT meta_value FROM " . $wpdb->prefix . "postmeta
WHERE meta_key = 'post_rating_0'
AND post_id = $pagina_id_test", ARRAY_A);
// define reviews array
$reviews_count = array();
// iterate through meta_values, count the occurence of each review
foreach ($reviews_posts as $reviews_post) {
if (isset($reviews_count[$reviews_post['meta_value']])) {
$reviews_count[$reviews_post['meta_value']] = $reviews_count[$reviews_post['meta_value']] + 1;
} else {
$reviews_count[$reviews_post['meta_value']] = 1;
}
}
// echo results
$aantal_reviews = 0;
foreach ($reviews_count as $review) {
$aantal_reviews++;
}
echo $aantal_reviews;

global $wpdb;
global $post;
$post_id = $post->ID;
$c = $wpdb->get_row("SELECT COUNT(*) as NBR FROM {$wpdb->postmeta}
WHERE post_id={$post_id} AND meta_key='post_rating_0' AND
(SELECT ID FROM {$wpdb->posts} WHERE ID={$post_id} AND post_type='page') IS NOT NULL");
echo $c->NBR;
Select count of post where post_type is page and meta_key='post_rating_0'

Related

Wordpress: sum custom meta_values from all posts by a user

Ok, so right now I have a working code, that basically echo the total amount of posts for meta_key _heart_this for a specific user:
<?php $query = new WP_Query( array( 'meta_key' => '_heart_this', 'author' => 1, ) );
echo $query->found_posts; ?>
However, some posts have meta_value of 2, 3 etc..
So I want to sum the total amount of meta_value and echo, because right now the number I get is inaccurate obviously.
I'm almost there, need a bit of guidance.
Thanks in advance.
It should be taken in loop. Use below code
$query = new WP_Query( array( 'meta_key' => '_heart_this', 'author' => 1) );
//Take all values in array
$sum = [];
if( $query->have_posts()){
while( $query->have_posts()): $query->the_post();
{
//Get all posts values one by one
$value = get_post_meta( get_the_ID(), '_heart_this', true );
//Push values in array
array_push($sum, $value);
}
endwhile;
}
//Sum all values of array to get total amount of meta value
$sum_final = array_sum ($sum);
echo $sum_final;
Tested & works

How to increment posts within a specific category for every 6 posts of all categories

I am successfully returning a post from the 'CTA' category every 6 posts on the page. However every time the loop is run I get stuck with the first post in the array instead of the incremental progression of all the CTA posts.
I've tried returning the posts by RAND order and that worked better but did have repeats. I don't seem to be able to get the count set for the CTA category after already setting a counter for every six posts.
<?php if ( $query->have_posts() ) { ?>
<?php while ($query->have_posts()) {
if( $query -> post_count > 0 ) {
$postnum = 0;
foreach( $query -> posts as $post ) {
$postnum++;
if( $postnum%5 == 0 ) {
$args = array( 'cat' => 1824, 'posts_per_page' => 1, );
query_posts( $args );
$current_post = 0;
while ( have_posts() ) : the_post();
$current_post++;
echo "CTA Card Specific Info";
endwhile;
}
$query->the_post();
?>

wordpress custom query is too slow

I am trying to create a custom filter with the custom table in woocommerce. there is approx 24 000 products that makes the custom filter very slow. How to make it faster?
here is my code:
function custom_wpquery( $query ){
if(isset($_GET['min-price'])){
// the main query
global $wp_the_query;
global $wpdb;
if ( 'product' === $query->get( 'post_type' ) ) {
$rr = g($_GET['min-price'],$_GET['max-price']);
foreach($rr as $rrr){
$fabricc = $wpdb->get_results("SELECT * FROM CWF_posts WHERE post_title = '".$rrr."' AND post_type = 'product'");
foreach($fabricc as $fabriccc){
$cID[] = $fabriccc->ID;
}
}
//print_r();exit;
// foreach($cID as $cIDD){
// $cat= wp_get_post_terms( $dd->ID, 'product_cat' );
// }
//$dd = get_page_by_title( $rrr, OBJECT, 'product' );
// $include[]=$dd->ID;
//
// $c[] = $cat[0]->slug;
//$c2[] = $cat[1]->slug;
$query->set('post__in', $cID);
}
}
}
add_filter( 'pre_get_posts', 'custom_wpquery' );
Thanks
You can create a multiple-index on fields that you are querying... Since your one field is constant I advise that convert your SQL to
SELECT * FROM CWF_posts WHERE post_type = 'product' AND post_title = '".$rrr."'
and create an index on post_type and post_title with this command on MySQL
CREATE INDEX type_title_index on CWF_posts (post_type, post_title)
also you can check
Understanding multiple column indexes in MySQL query
https://dev.mysql.com/doc/refman/8.0/en/create-index.html

Sum custom post meta values

This should be really straightforward..
I have a custom post type: "shipment" with a custom field "shipment_cost"
I want to sum the total cost of all shipments.
I've managed to get it to output the correct amount with a single post:
$totalcost = 0;
$post_ids = [2583];
foreach( $post_ids as $post_id )
{
$totalcost += (int) get_post_meta( $post_id, 'shipment_cost', true );
}
echo $totalcost;
But when I try to use all shipment CPTs in an array, I get a result of zero.
$shipments = array(
'post_type' => 'shipment'
);
$totalcost = 0;
$post_ids = $shipments;
foreach( $post_ids as $post_id )
{
$totalcost += (int) get_post_meta( $post_id, 'shipment_cost', true );
}
echo $totalcost;
Is my shipments array wrong, or am I just going about this the wrong way in general?
Thanks!
Right now it seems that you are using $shipments as it was an array of post ids? You first need to retrieve all post ids:
$shipments = get_posts(array(
'post_type' => 'shipment',
'post_status' => 'publish'
));
Since the above would return post object you need to modify your loop:
foreach ($shipments as $post) {
$totalcost += (int) get_post_meta($post->ID, 'shipment_cost', true);
}
A better and faster way might be to use a raw database query to get the sum for all meta values:
$totalcost = $wpdb->get_col("SELECT SUM(pm.meta_value) FROM {$wpdb->postmeta} pm
INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE pm.meta_key = 'shipment_cost'
AND p.post_status = 'publish'
AND p.post_type = 'shipment'");
This just saved my life!! Been looking for a solution to total inputs from a Pods Custom Post Type which has a Custom Field that users can enter a number into. I then wrapped the code in a shortcode to display on front end
function jobs_shortcode () {
$jobs = get_posts(array(
'post_type' => 'pledged_job',
'post_status' => 'publish'
));
$total = 0;
$post_ids = $jobs;
foreach ($jobs as $post) {
$total += (int) get_post_meta($post->ID, 'jobs_pledged', true);
}
echo $total;
}
add_shortcode( 'jobs', 'jobs_shortcode' );
THANK YOU!

Fastest approach: Count all products (including variations) in WooCommerce

I'm wondering if there exist any faster approaches to count all products in WooCommerce (and their child-variations), than this following code-example:
function total_product_count() {
$product_count = 0;
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
/* Initialize WP_Query, and retrieve all product-pages */
$loop = new WP_Query($args);
/* Check if array contains any posts */
if ($loop->have_posts()):
while ($loop->have_posts()):
$loop->the_post();
global $product;
/* Count the children - add count to product_count */
product_count += count($product->get_children());
endwhile;
endif;
return $product_count;
}
On my local XAMPP web-server the function executes in 3 seconds (having 253 products), which is way too long time. The function returns 1269.
$product->get_children() is not always correct in this situation. get_children() return child products as well as grouped products if exist.
I hope the better and fastest way is to use wpdb:: MySQL query method. Try the following code
echo 'Number of main products => '.main_product_count();
echo 'Number of variations => '. variation_product_count();
function main_product_count() {
global $wpdb;
$count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE `post_type` LIKE 'product'");
return $count;
}
function variation_product_count() {
global $wpdb;
$count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE `post_type` LIKE 'product_variation'");
return $count;
}
as your naming
$loop =new WP_Query($args);
try this code
$loop->post_count;
while ($loop->have_posts()){
global $product;
/* Count the children - add count to product_count */
$loop->post_count += count($product->get_children());
}
WP_Query has a property which gives you the number of posts found matching the current query parameters:
function total_product_count() {
$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
$products = new WP_Query( $args );
return $products->found_posts;
}

Categories