How to cache the results of a query and display the cached results

I have a website centered around music. I have 3 main post types- 'Artist', 'Release', and 'Version'. There are 4 types of 'Version'- album, single, video, misc. Artist is parent of Release. (artist has a single -> many relationship with releases) Release is a parent of Version. (Release has a single-> many relationship with version) I've established these relationships in the plugin toolset types. In my single-artist page, I have releases displayed in sections. Here's an example of the code …
Category: Web

Counting number of identical meta keys

Im looking to count how many meta keys exist within a post, because each time a specific user action happens, a new meta key is created with a date in it. I have this, which does not return anything (I'm looking for a number)... ... $post_title = $row->post_title; $id = $row->ID; $post_count = $wpdb->get_var(" SELECT COUNT(DISTINCT $wpdb->postmeta.meta_key) FROM $wpdb->posts LEFT JOIN $wpdb->postmeta WHERE $wpdb->posts.post_type = 'things' AND $wpdb->postmeta.meta_key = 'dates' AND post_id = $id "); echo $post_title . ' (' …
Category: Web

Count posts in category including child categories

I have found this code somewhere, it counts posts but only which are under specific category. I would appreciate if someone could expand this so it would count all posts which also belong to its child categories? function wp_get_cat_postcount($id) { $cat = get_category_by_slug( $id ); $count = (int)$cat->count; $taxonomy = 'category'; $args = array( 'child_of' => $id, ); $tax_terms = get_terms($taxonomy,$args); foreach ($tax_terms as $tax_term) { $count +=$tax_term->count; } return $count; }
Category: Web

Return only Count from a wp_query request?

Is it possible with the standard build-in tools in Wordpress to get wp_query to only return the count of an query? Right now, I’ve a query which has several meta_queries but what the only thing I’m interested in is the actually count of the query. I know that I can use the found_posts property but the query itself generates a big overhead by query SELECT * and thus returns the whole object. I could just as easy query the DB …
Category: Web

Double count view in archive.php

I'm having a hard time using this snippet I'm getting double views each time the post is queried. This is in my functions.php file: function getPostViews($postID){ $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); return "0"; } return $count; } function setPostViews($postID) { $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ $count = 0; delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); }else{ $count++; update_post_meta($postID, $count_key, $count); } } // Remove issues with prefetching adding extra …
Category: Web

How do I find the count of the current post?

Let's say my custom post type is 'story'. I've got 20 stories published on my site. So each post becomes: 'story 1', 'story 2', ... 'story 20'. Thus, first post is 'story 1', and last post is 'story 20'. Now when I open one of these custom posts, I want to see the number. Example, if I open the ninth (published) post I want to see it mentions 'story 9'. I don't want to hardcode it which I can do …
Category: Web

How to update incorrect post count in taxonomy?

I have imported a large amount of content using WPAllImport, all to a custom post type called "article" ("Articles") and all organised by a custom taxonomy type called "source" ("Sources"). However, on the edit-tags.php page for the Sources taxonomy listing, the Articles post counts are all inaccurate. There is one term which shows as only having three Articles against it but, on the post index for Articles with that Source, it clearly has 1,997. How can I fix all the …
Category: Web

number of posts with "Load More"

I am using the plugin Ajax Load More to query posts and have a filter system to thin out the posts in the loop. I am trying to add a counter that will say "Your search returned X amount of posts" Where X is the number of posts displayed/matching the current criteria. I was able to grab the total number of posts, but wasn't able to grab the current number of posts being displayed via Ajax Load More. Wasn't able …
Category: Web

Click Count on Download a File

i have website with woocommerce for digital download. I make all free for download, and this my code for download the file: $downloads = $product->get_files(); foreach( $downloads as $key => $each_download ) { echo '<a href="'.$each_download["file"].'">Download</a>'; } My question, how to make click count in single product page if people click that link for download the file? I want to showing how many the file already downloaded... Can you tell me how make code for that? Thank you... EDIT: Hi, …
Category: Web

Counting number of posts in multiple (sub)categories using shortcode

I'm making a new cycling team website and on the home page i have 3 different type of stats: "Win, Podium and top-10" Now i want the number of the stat go up if i post something with for instance the category: "Podium" But i want it to go 2 up if i select the categories: "1-podium / 2-podium" ETC So in short, i want the count number go up if i post something while selecting one or more categories. …
Category: Web

custom post type category count shortcode

I'd like to create a shortcode that would display the post count for a specific category within a custom post type. I've been trying to alter the code below but need some help. Could anyone let me know how to tweak so that the shortcode is for a specific custom post type. // Add Shortcode to show posts count inside a category function category_post_count( $atts ) { $atts = shortcode_atts( array( 'category' => null ), $atts ); // get the …
Category: Web

Fastest way of counting posts of a custom post type in a specific taxonomy term?

I know this kind of question is legion but I have read through all related topics and I can't find a satisfying solution. What I want to achieve is simple: on my taxonomy (author) term archive page I have a list of all terms within that taxonomy from which you can select to switch from one term to another. I want to display the number of posts only from the post type (several CPT are associated with author). Simply put …
Category: Web

divide custom field values in div every two values

i have this html mark-up to sort some data <div class="row"> <div class="col-xs-2"> <img src="images/member-schools/ms1.jpg" class="img-responsive"> </div> <div class="col-xs-4"> <h4>American International School - East</h4> </div> <div class="col-xs-2"> <img src="images/member-schools/ms2.jpg" class="img-responsive"> </div> <div class="col-xs-4"> <h4>Dover American International School</h4> </div> and i am pulling out the data from repeatable group metabox i use from CMB2 this is the code for the registered metabox function schools() { // Start with an underscore to hide fields from custom fields list $prefix = 'schools_'; /** …
Category: Web

Count Post and Page Views based on meta_value Using Shortcode in Dashboard Widget

I'm working on a counter that, each time a post is viewed, the meta value is updated by 1. This works fine, but I cannot seem to figure out how to make it count posts and pages, including the home page (is_front_page). This is the code: function counter_posts( $post_id ) { if ( is_user_logged_in() && current_user_can( 'administrator' ) ) return; $count_key = 'count_views'; $count = get_post_meta( $post_id, $count_key, true ); if ( $count == '' ) { $count = 0; …
Category: Web

ACF: How to query for a given value count of an array like field? (e.g.: How many rows has a `flexible_content` field?)

How can I write a WordPress meta query that gives me an array of users/posts that have a given amount of entries in a custom field that stores multiple values? Something like SQL COUNT(), but for the number of entries in an ACF array field. When writing a WP_Meta_Query and comparing with >, >=, etc. this doesn't seem to work for me, perhaps because the data is stored serialized. So, basically, how can I perform a simple selection of records …
Category: Web

Show the number of the post

I'd like to show the Number of the post near the post in a category. It's simpler to explain with a drawing than with words: My first solution was a simple counter inside the loop, but it doesn't work with pagination and puts the number 1 at the more recent post and not at the oldest. Is something like this possible? How? Thanks!
Category: Web

Counting a WP_Post Object value in an arary, using a filter?

I am trying to check whether a post in the array is the first post with a certain array value. I have a code similar to the one below: Plugin code <?php function my_function() { $entries = $posts->posts; foreach( $entries as $index => $entry ) { $title = get_the_title( $entry->ID ); $event = $title; $output = apply_filters( 'my_filter', $event, $entries, $index ); } return $output; } ?> Code in my child theme functions <?php function add_stuff( $event, $entries, $index ) …
Category: Web

How to calculate posts number?

I want to achieve something like this: foreach ( $posts as $post ) { if ( $a = $b ) ) { // if true +1 to the result; } else { // if not true -2 to the result; } } echo 'Your result is X'; How to replace X with the result of a calculation? How calculation code should look?
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.