Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have problem in my first theme design, and I think you can help me easily!(I searched my problem several times in Google and WordPress codex but I cant find exactly that I want!!)

I want to displaying my "yesterday's last post's Custom filed, from Unique terms of one Taxonomies" in a cell of table!

In another cell I want to display "last post's Custom filed, from Unique terms of one Taxonomies"

I use this code:

<?php
/**
* @package WordPress
* @subpackage Default_Theme
*/
    $yesterday = strtotime("-1 day");
    $yesterday = date("Y-m-d", $yesterday);
    $today = date("Y-m-d");
$querystr = "SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id
    AND wposts.post_date >= '$yesterday'
    AND wposts.post_date <= '$today'
    AND wposts.post_type = 'MY-CUSTOM-POST-NAME'
    ORDER BY wposts.post_date ASC;";
$posts = $wpdb->get_results($querystr, OBJECT);
get_header();
?>
<div  id="content" role="main">
<?php 

if($posts) {
    foreach($posts AS $post) setup_postdata($post);
    {
        ?>
<div id="post-<?php  the_ID(); ?>">
<div> <?php  echo get_post_meta($post->ID,'MY-CUSTOM-FIELD',TRUE) ?></div>
</div>
<?php  } ?>
<?php  } else { ?>
<h2>Not Found</h2>
<p>Sorry, but you are looking for something that isn't here.</p>
<?php  get_search_form(); ?>
<?php  } ?>
</div>
<?php  get_sidebar(); ?>
<?php  get_footer(); ?>

but this code display my "last Custom filed from ALL terms of one Taxonomies!"

Please Help me to resolve this.

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

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;
}
share|improve this answer
    
Thank you @rfrq i think this code is helping me but: Please let me know where can i input my cpt, tax, term_slug & cf in this code! because i replaced $cpt with my custom post type name and $tax... but nothing happened.(blank page)?? my cpt=portfolio / tax=web / term_slug=php / cf = year .... –  Kian Abrishami Jul 1 '13 at 13:54
    
You have to copy the whole function as it is into your themes functions.php . Then call show_latest48h_post_cf_in_term( $cpt, $tax, $term_slug, $cf ) anywhere in your theme. In your case show_latest48h_post_cf_in_term( 'portfolio', 'web', 'php', 'year' ) should work. –  rfrq Jul 1 '13 at 15:38
    
WOW! Thank you very much its working now ;) BUT dear @rfrq this code i worked but doesn't show my current local time its work with UTC time! –  Kian Abrishami Jul 2 '13 at 6:30
    
Now the function uses WordPress' current_time() function which relates to your blog settings. I also improved speed +120-160% by using get_posts instead of WP_Query. I tried to find a solution which takes care of the visitors timezone and experimented a lot but I always got UTC. –  rfrq Jul 3 '13 at 0:09
    
dear @rfrq i want show last post's date&time (local time/(GMT + 4:30)) publish date –  Kian Abrishami Jul 8 '13 at 13:52
show 2 more comments

First, use WP_Query rather than your own SQL.

I think this is what you want:

$yesterday = strtotime("-1 day");
$yesterday = date("Y-m-d", $yesterday);
$yesterday = explode('-',$yesterday);
$args = array(
  'post_type' => 'MY-CUSTOM-POST-NAME',
  'year' => $yesterday[0],
  'monthnum' => $yesterday[1],
  'day' => $yesterday[2],
  'tax_query' => array(
    array(
      'taxonomy' => 'people',
      'field' => 'slug',
      'terms' => 'bob'
    )
  )
);
$query = new WP_Query($args);

Some explanation:

If what you want is posts from "yesterday" you don't really need to check for posts between today and yesterday, you just ask for posts from yesterday.

When you say you want "from Unique terms of one Taxonomies", I really don't know what you mean but I think that you mean you want posts from some particular term or terms from some particular taxonomy, hence the tax_query.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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