How WordPress Displays the number of latest articles in a week for a specified category

Source: Internet
Author: User

Title, want to write a navigation bar, each category in the navigation bar has a bubble that shows the number of articles updated within a week.
The total number of articles known to get a category is


  
   

How do i show how many articles are published within a week? Thank you..
The approximate effect is similar to station B.

Reply content:

Title, want to write a navigation bar, each category in the navigation bar has a bubble that shows the number of articles updated within a week.
The total number of articles known to get a category is


  
   

How do i show how many articles are published within a week? Thank you..
The approximate effect is similar to station B.

I haven't seen Worpdress's problem for a long time.

I happen to be 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 (' 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 ', ' po ' Sts_per_page ' =>-1); $query = new Wp_query ($args); return $query->post_count;}

Due to the use date_query of this, so this only applies to 3.7+

--
Update:
To wp_query add some parameters to optimize.
Generated SQL, that's about it.

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, it is recommended to use the above SQL, with $wpdb->get_var ($sql).

Remember to use COUNT (wp_posts.id), after all, the aggregation function is less expensive.

Also give you a train of thought.
Put it in the 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 the specified category under the specified number of articles, category ID number can be seen in the WordPress background

newArticle($num,$cat); // $num 要显示的数量;  $cat 制定分类的ID

Specify the number of latest articles:

newArticle($num,null); // $num 要显示的数量;  注意:null 一定要填写,否则出现PHP报错

After the call is complete. You can write a piece of code yourself, the idea is almost: you need to set a time, such as seven days in the classification of the latest article. WordPress default should not be this feature. You can query in the database, how much content, and then cache to a SQL form, the WordPress foreground read the value of this form.

Alternatively, set a cardinality (such as 0) for the number of hints for each category. Then read, this category updates the number of articles below (provided the time range control) and then add this value on the base.

Give you a 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


  
   

Variable ID is the ID of the category

A brutal fact is that the classification (category, a type of term in a data table) has no meta-auxiliary data.

So you can't simply record this number in a category with one function as you would record post meta, and then use another function to tune it out. This is not a thing to do.

The feasible way is: first use the background of the scheduled task, their own $wpdb database operation class, constructs the SQL statement query this number. Then use wp_options the table cache, which is called every time the foreground is displayed.

Required API includes: WordPress Cron, WordPress Options, class/wpdb.

Note: Sorry, actually there is no time to write this SQL (escape) The main idea is: Query posts + term_relationships Two tables (join relationship needs to consider), filter a specific classification (term) all post records, by post type (POST), The publication status (publish) and the date (>= specific date) are filtered, and the last select count is counted.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.