For example, if you want to write a navigation bar, each category in the navigation bar has a bubble that shows the number of updated articles within one week. It is known that the total number of articles for retrieving a category is {code...}. How can I display the number of articles published within one week? Thank you. The general effect is similar to that of site B.
For example, if you want to write a navigation bar, each category in the navigation bar has a bubble that shows the number of updated articles within one week.
It is known that the total number of articles for retrieving a category is
How can I display the number of articles published within one week? Thank you ..
The effect is similar to that of Station B.
Reply content:
For example, if you want to write a navigation bar, each category in the navigation bar has a bubble that shows the number of updated articles within one week.
It is known that the total number of articles for retrieving a category is
How can I display the number of articles published within one week? Thank you ..
The effect is similar to that of Station B.
I haven't seen any problems with worpdress for a long time.
I am very familiar with wordpress development.
This is the function I wrote.
function get_this_week_post_count_by_category($id){ $date_query = array( array( 'after'=>'1 week ago' ) ); $tax_query = array( array( 'taxonomy' => 'category', 'field' => 'id', 'terms' => $id ) ); $args = array( 'post_type' => 'post', 'post_status'=>'publish', 'tax_query' => $tax_query, 'date_query' => $date_query, 'no_found_rows' => true, 'suppress_filters' => true, 'fields'=>'ids', 'posts_per_page'=>-1 ); $query = new WP_Query( $args ); return $query->post_count;}
Becausedate_querySo this only applies to 3.7 +
--
Update:
Towp_queryAdd some parameters for optimization.
The generated SQL is probably like this
SELECT wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( ( post_date > '2014-02-04 10:47:10' ) ) AND ( wp_term_relationships.term_taxonomy_id IN (247) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC
If the data is large, we recommend that you use the preceding SQL statement with $ wpdb-> get_var ($ SQL ).
Remember to use count (wp_posts.ID). After all, the aggregate function has a low overhead.
In addition, I will give you an idea.
Put it in functions. php.
function newArticle($num,$cat){ $args=array( 'posts_per_page' => $num, 'cat' => $cat, 'order' => 'desc' ); $posts = query_posts($args); if( have_posts() ) : $html = ''; $html .= '
'; foreach($posts as $post) : $html .= '
- '; $html .= 'ID).'" rel="bookmark" title="'.$post->post_title.'">'.$post->post_title.''; $html .= '
'; endforeach; $html .= '
'; $html .= ''; endif; echo $html;}
Call a specified number of articles under a specified category. The category ID is displayed in the wordpress background.
NewArticle ($ num, $ cat); // $ num indicates the quantity to be displayed. $ cat specifies the category ID.
Specify the number of latest articles:
NewArticle ($ num, null); // $ num indicates the quantity to be displayed. Note: null must be filled in; otherwise, an error is reported in PHP.
After the call is completed. You can write a piece of code by yourself. The idea is similar: You need to set a time, such as the latest article about this category within seven days. Wordpress does not have this function by default. You can query in the database and cache the content to an SQL form. The wordpress foreground reads the value of this form.
Or, set a base (for example, 0) for the number of prompts for each category ). Then, read the number of articles updated under this category (the time range is controlled on the premise) and add this value on this base.
For your reference,
function wt_get_category_count($input = '') { global $wpdb; if($input == '') { $category = get_the_category(); return $category[0]->category_count; } elseif(is_numeric($input)) { $SQL = "SELECT {$wpdb->term_taxonomy}.count FROM {$wpdb->terms}, {$wpdb->term_taxonomy} WHERE {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id AND {$wpdb->term_taxonomy}.term_id = {$input}"; return $wpdb->get_var($SQL); } else { $SQL = "SELECT {$wpdb->term_taxonomy}.count FROM {$wpdb->terms}, {$wpdb->term_taxonomy} WHERE {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id AND {$wpdb->terms}.slug='{$input}'"; return $wpdb->get_var($SQL); }}
Call
The variable ID is the classification ID.
A cruel fact is:Category (a type of data table that is classified as a term) does not have meta-assisted data.
Therefore, you cannot simply record this number in a category using one function, just like recording Post Meta, and then call another function. This cannot be done.
A feasible solution is to first use the background to plan tasks and use them on your own.$wpdbDatabase Operation class, construct an SQL statement to query this number. Then usewp_optionsTable cache, called directly at the front-end display each time.
Required APIs include WordPress Cron, WordPress Options, Class/wpdb.
Note: Sorry, I don't have time to write this SQL statement (escape). The main idea is to query the posts + term_relationships tables (consider the JOIN relationship) and filter a specific category (term) all post records are filtered by the post type (post), publish status (publish), and date (> = specific date), and the last select count is counted.