1

I am using Custom Metaboxes and Fields for Wordpress

I am using the datetime_timestamp custom meta with a page. I am trying to echo the title of the page I am using the custom meta datetime in. But it is not working. I have the code incorrect. And I may possibly be far off!

I have the code in the header, and I want to display 1 with the closest datetime.

global $post;
$text = get_post_meta( $post->ID, '_cmb_test_datetime_timestamp', true );
$time = current_time( 'Y-m-d' );
if ($text <= $time)
  echo $post->post_title;

It is displaying the title of the most recent title when viewing a page of post. Or if I am viewing a single post page it displays that post title.

I think I might need to make a custom loop possibly. I am a bit lost.

  • Do you want to display the title of the page with the most recent _cmb_test_datetime_timestamp, in the header of whatever page or post you're on, regardless? – bonger Dec 21 '14 at 23:01
  • Yes - that is correct. It is intended to be in the header and displayed at all times. The pages are "event" pages and I want them to be able to read that first thing when they read from top to bottom. – wannabe programmer Dec 22 '14 at 14:17
1

This should output the title of the page with the most recent timestamp:

$args = array(
    'post_type' => array( 'page' ),
    'order' => 'DESC',
    'orderby' => 'meta_value_num', // Note was `meta_value` before
    'meta_key' => '_cmb_test_datetime_timestamp',
    'numberposts' => 1,
);
$posts = get_posts( $args );
if ( $posts ) echo $posts[0]->post_title;
wp_reset_postdata();

To add conditions, use a meta_query clause, eg

$midnight = strtotime( date( 'Y-m-d', time() + DAY_IN_SECONDS ) );
// If setting timezone when saving data, $midnight += get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
$args = array(
    'post_type' => array( 'page' ),
    'order' => 'DESC',
    'orderby' => 'meta_value_num', // Numeric sort.
    'meta_query' => array(
        array(
            'key' => '_cmb_test_datetime_timestamp',
            'compare' => '<',
            'value' => $midnight,
            'type' => 'UNSIGNED', // Note was missing before.
        ),
    ),
    'numberposts' => 1,
);
  • Awesome that worked. I know there is always several ways of writing this. I need to be able to link to that page also. With the way its written I dont see how I can wrap the permalink around the title. Any suggestions? I was just reading up on post_type page when this notification came through. I was trying to figure out if I could just use the _cmb custom meta as a "meta_key" value. As you have shown you can. – wannabe programmer Dec 22 '14 at 14:51
  • how can I change the args to only show one based one based on the meta _cmb_test_datetime_timestamp and the current date? Then it would show the next page with the meta after that date has passed. – wannabe programmer Dec 22 '14 at 18:00
  • You'd need to add a meta_query clause - I'll update the answer with an example... – bonger Dec 22 '14 at 18:28
  • Thank you, but it isn't working, I had something very similar to this. I have found tons of examples working with orderby meta_val_num and then the meta_query with the compare and value keys. But let me ask this, would it not work since there is not an actual date field in the database for this meta? Here is a screen shot of this record in the db imgur.com/KvKk238 – wannabe programmer Dec 22 '14 at 20:50
  • Ok so it's not a timestamp, it's a date, so you need meta_value not meta_value_num, and date() instead of time(), but it's in a poor format to do sorting and comparing on, it should be stored in the DB as YYYY-MM-DD to make things easy, and then formatted for display. – bonger Dec 22 '14 at 21:29

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.