0

I can think of a number of ways to go about doing this. I'm looking for the most efficient way. Menu looks like this:

catArchive1 [num_new_posts] catArchive2 [num_new_posts] catArchive3 [num_new_posts]

^^[num_new_posts] being the number of new posts

Assuming we have a cookie value set for the users last visit $_COOKIE['lastvisit'].

I could do something like the follow for EACH menu/category

$args = array(
 'category' => $cat_id,
 'posts_per_page' => -1,
 'date_query'     => array( 'after' => $_COOKIE['lastvisit'] ),
 );

$new_posts = get_posts($args);
$num_posts = count($num_posts); // Number of new posts

Of course, this is another query for each menu item. Any ideas how to combine this into one and still know how many new posts in each category?

  • A single query for all categories, since lastvisit? Would require more code to iterate through the result manually but save on the expensive multi-query approach. – jdm2112 Aug 30 '16 at 16:10
0

use wpdb query instead: "SELECT DISTINCT wposts.* FROM $wpdb->posts wposts LEFT JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE post_date >= {$lastvisit} AND $wpdb->term_taxonomy.taxonomy = 'category' ";

I did not test this but should work. I also suggest you to use prepared statements

0

Ended up doing one query and looping results and organizing.

/**
* Calculates number of posts since a users last visit.
* @return array Organized by category and total.
*/
function denverite_posts_by_cat_since_last_visit(){
session_start();
$post_counts = array();

// Set session value as the time stamp of the users last visit if not yest set.
// use this value to get "new posts" during users sessions. 
if(!isset($_SESSION['denveritelastvisit'])){
    $_SESSION['denveritelastvisit'] = $_COOKIE['denveritelastvisit'];
}

if ($_SESSION['denveritelastvisit']) {

    $lastvisit = date('Y-m-d G:i:s', $_SESSION['denveritelastvisit']);
    $args = array(
        'posts_per_page' => -1,
        'post_type' => 'post',
        'date_query' => array(
            'after' => $lastvisit 
        )
    ); 
    $new_posts = get_posts( $args );

    // all posts since visitors last visit. 
    $post_counts['total_posts'] = count( $new_posts );

    // Loop all posts and count posts for each category.
    foreach ( $new_posts as $new_p ) {
        $cats = get_the_category( $new_p->ID );

        if (is_array( $cats )) {

            foreach ( $cats as $cat ) {

                if ( in_array($post_counts[$cat->slug], $post_counts) ) {
                    $post_counts[$cat->slug] = $post_counts[$cat->slug] + 1;
                } else {
                    $post_counts[$cat->slug] = 1;
                }

            }

        }

    }
 }
 // Overwrites last visit cookie 
 denverite_lastvisit_set_cookie();
 return $post_counts;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.