• Try this for some time and a big headache for me. So I want to show 2 pages that have custom data field (example: 1 Martie 2013) – in Romanian and the custom field value is greater than today.

    For example:

    Page 1 – custom field “launch date” is “1 Martie 2013”
    Page 2 – custom field “launch date” is “1 Martie 2013”
    Page 3 – custom field “launch date” is “1 Martie 2013”

    We are now in “2 Martie 2013” and I want to show only pages 2 and 3.

    Now, I try this

    <?php
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $today = date('j F Y');
    query_posts(array(,
    	'posts_per_page' => 2,
    	'paged' => $paged,
    	'meta_key' => 'data-lansare',
    	'orderby' => 'meta_value',
    	'order' => 'ASC',
    	     'meta_query' => array(
    		array(
    		'key' => 'data-lansare',
    		'meta-value' => $value,
    		'value' => $today,
    		'compare' => '>=',
    		'type' => 'CHAR'
    		)
    	)
    ));
    if (have_posts()) :
    while (have_posts()) : the_post();
    ?>

    not working!

    I try to change custom field date to format YYYYMMDD edit couple of pages custom field “data-lansare” (ex: 20130301) and change $today = date(‘j F Y’); with $today = date(‘Ymd’); but again nothing works.

    A little help.

Viewing 15 replies - 16 through 30 (of 44 total)
  • Thread Starter Alin Ionut

    (@c3dry2k)

    Let’s say I’ll modify them somehow, but now how I retrive that custom field value to display from Y-m-d to j F Y?

    This line of code

    <?php if ($custom_date = get_post_meta($post->ID,'data-lansare',true)): ?>
      - <img src="https://www.digitalgames.ro/calendar.png" alt="Data de lansare" width="14" height="14" title="Dat? de lansare" style="vertical-align:top;"> <span style="color:#FF0000"><?php echo $custom_date; ?></span>
    Moderator keesiemeijer

    (@keesiemeijer)

    That will be more difficult. Can you provide all the month names in your language from January to December.

    Thread Starter Alin Ionut

    (@c3dry2k)

    Ianuarie
    Februarie
    Martie
    Aprilie
    Mai
    Iunie
    Iulie
    August
    Septembrie
    Octombrie
    Noiembrie
    Decembrie

    Moderator keesiemeijer

    (@keesiemeijer)

    Before we do anything you have to make a backup of your database.
    https://codex.www.ads-software.com/Backing_Up_Your_Database

    Because the query gets the posts based on a date with numbers and your custom fields have month names in them the only way to deal with this is to change the month names in the database itself.

    Thread Starter Alin Ionut

    (@c3dry2k)

    I have backup my database.

    Thread Starter Alin Ionut

    (@c3dry2k)

    I solve the problem of editing them, now I want to display in pages custom field “date-lansare” not as 2013-11-17, but as 17 Noiembrie 2013, as I say above.

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with:

    <?php echo ucwords( date_i18n( 'j F Y', strtotime( $custom_date ) ) ); ?>

    Thread Starter Alin Ionut

    (@c3dry2k)

    You made my day man, thank you.

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome. I’m glad you’ve got it resolved.

    Thread Starter Alin Ionut

    (@c3dry2k)

    I want another small thing

    I want to retrieve the same pages with that custom field date “data-lansare” but to filter them by: show just last month, show next month, past 30 days, this month, next year. It is possible?

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with the ‘BETWEEN’ parameter:
    https://codex.www.ads-software.com/Function_Reference/WP_Query#Custom_Field_Parameters

    Example for the current month:

    $start_curr_month = date("Y-m-01");
    $end_curr_month = date("Y-m-t");
    
    $args= array(
        'post_type'      => array( 'page' ),
        'posts_per_page' => 2,
        'meta_key'       => 'data-lansare',
        'orderby'        => 'meta_value',
        'order'          => 'ASC',
        'meta_query' => array(
            array(
                'key'     => 'data-lansare',
                'value'   => array( $start_curr_month, $end_curr_month ),
                'compare' => 'BETWEEN',
                'type'    => 'DATE',
            )
        )
    );

    Thread Starter Alin Ionut

    (@c3dry2k)

    sorry, not so good with php… this one work well. thanks

    Thread Starter Alin Ionut

    (@c3dry2k)

    can you help me and with the rest of the codes?

    Moderator keesiemeijer

    (@keesiemeijer)

    Put these functions in your theme’s functions.php file: https://gist.github.com/keesiemeijer/7563447

    Get the dates for your meta query like so:

    // example current month
    $date_args = array(
    	'type'        => 'month', // month - year, month, day
    	'start_from'  => 'type', // today, type - Start from today, or from first/last day of month/year
    	'future_past' => 'past', // past, future - Past or future date range
    	'offset'      => 0, // 0 = current month (1 = one month from start date etc...)
    	'duration'    => 1, // duration of 1 month (1 or higher)
    );
    // get the BETWEEN dates
    $dates = get_between_clause_dates( $date_args );
    
    $args= array(
        'post_type'      => array( 'page' ),
        'posts_per_page' => 2,
        'meta_key'       => 'data-lansare',
        'orderby'        => 'meta_value',
        'order'          => 'ASC',
        'meta_query' => array(
            array(
                'key'     => 'data-lansare',
                'value'   => $dates,
                'compare' => 'BETWEEN',
                'type'    => 'DATE',
            )
        )
    );

    If you set the ‘start_from’ parameter to ‘type’ you get whole months (or years). The result of the get_between_clause_dates() function in the code above (on November 20 2013) is:

    array( '2013-11-01', '2013-11-30' )

    With ‘start_from’ set to ‘today’ the date starts at the current date.

    // example on November 20 2013
    $date_args = array(
    	'type'        => 'month', // month - year, month, day
    	'start_from'  => 'today', // today, type - Start from today, or from first/last day of month/year
    	'future_past' => 'past', // past, future - Past or future date range
    	'offset'      => 0, // 0 = current month (1 = one month from start date etc...)
    	'duration'    => 1, // duration of 1 month (1 or higher)
    );
    $dates = get_between_clause_dates( $date_args );

    Results in in an array like this:

    array( '2013-10-20', '2013-11-20' )

    Here are the args if you want the next full month (use an offset):

    // example on November 20 2013
    $date_args = array(
    	'type'        => 'month', // month - year, month, day
    	'start_from'  => 'type', // today, type - Start from today, or from first/last day of month/year
    	'future_past' => 'future', // past, future - Past or future date range
    	'offset'      => 1, // 0 = current month (1 = one month from start date etc...)
    	'duration'    => 1, // duration of 1 month (1 or higher)
    );

    Which results in:

    array( '2013-12-01', '2013-12-31' )

    Example of getting the current year and the year before:

    // example on November 20 2013
    $date_args = array(
    	'type'        => 'year', // month - year, month, day
    	'start_from'  => 'type', // today, type - Start from today, or from first/last day of month/year
    	'duration'    => 2, // duration of 1 month (1 or higher)
    
    	// don't need to set all parameters (see defaults of the function)
    );

    Which results in:

    array( '2012-01-01', '2013-12-31' )

    I hope this makes sense.

    Thread Starter Alin Ionut

    (@c3dry2k)

    Everything works perfect, truly thank you for your time and patience. I see that this works even if the pages there not in a specifric category. But this is a problem, because if I put one of these codes inside that php widget, it display nothing. why?

Viewing 15 replies - 16 through 30 (of 44 total)
  • The topic ‘Display pages with cumstom field date upcoming’ is closed to new replies.