Display yesterday’s last post’s Custom field from Unique terms of one Taxonomies

I hope I understood everything correct:

  • display a specific custom field
  • from a custom post-type post
  • assigned to a specific term in a custom taxonomy
  • for todays and yesterdays latest entry

I can’t solve this task improving your SQL-statement, but you can try the following function using standard WordPress stuff:

function show_latest48h_post_cf_in_term( $cpt, $tax, $term_slug, $cf ) {        
                    /* vars */
        /******************************************************/
        $output="";
        $today_latest="";
        $yesterday_latest="";
        $timestamp = current_time( 'timestamp' );
        $today = strftime('%Y-%m-%d', $timestamp);
        $yesterday = strftime('%Y-%m-%d', strtotime('-1 day', $timestamp));
        $today_ar = date_parse($today);
        $yesterday_ar = date_parse($yesterday);

        /* today */
        /******************************************************/                 
        // args
        $args_today = array(
            'post_type' => $cpt,
            'tax_query' => array(
                array(
                        'taxonomy' => $tax,
                        'field' => 'slug',
                        'terms' => $term_slug
                )
            ),
            'year' => $today_ar["year"],
            'monthnum' => $today_ar["month"],
            'day' => $today_ar["day"],
            'meta_key' => $cf,
            'posts_per_page'  => 1
        );
        // get today post
        $today_query = get_posts($args_today);        
        if ($today_query) {                
                $today_latest .= '<p>' . $today . ': ' . get_post_meta( $today_query[0]->ID, $cf, true) . '</p>';                    
        }

        /* yesterday */
        /******************************************************/                 
        // args
        $args_yesterday = array(
            'post_type' => $cpt,
            'tax_query' => array(
                array(
                        'taxonomy' => $tax,
                        'field' => 'slug',
                        'terms' => $term_slug
                )
            ),
            'year' => $yesterday_ar["year"],
            'monthnum' => $yesterday_ar["month"],
            'day' => $yesterday_ar["day"],
            'meta_key' => $cf,
            'posts_per_page'  => 1
        );
        // get yesterday post
        $yesterday_query = get_posts($args_yesterday);        
        if ($yesterday_query) {
            $yesterday_latest .= '<p>' . $yesterday . ': ' . get_post_meta( $yesterday_query[0]->ID, $cf, true) . '</p>';                    
        }

        /* output */
        /******************************************************/            
        if ($today_query || $yesterday_query) {
            $output = $today_latest . $yesterday_latest;
        } else {
            $output="Nothing found";
        }
        echo $output;
}

error code: 523