0

This code show only less date or equal today date.

How I can to show expired posts ?

It's a code

add_action( 'pre_get_posts', 'custom_post_type_archive_eventos' );

function custom_post_type_archive_eventos( $query ) {

if( $query->is_main_query() && !is_admin()  ) {

        $today =  date('Y-m-d');

        $query->set( 'posts_per_page','6' );


        $query->set( 'meta_query', array(
        'relation' => 'OR',
        array(
            'key'       => 'Data_START',
            'value'     => $today,
            'compare'   => '>=',
            'type'      => 'DATE',
        ),
           array(
            'key'       => 'Data_END',
            'value'     => $today,
            'compare'   => '>=',
            'type'      => 'DATE',
        ),

      ));
    }
}

In finally I need to have to this order:

1) Not expired posts

2) Rest... (expired posts)

UPDATE

Exists 3 type of posts with and without custom fileds.

1) Post without custom fields

2) Post with only custom field :

Data_START : 2018-02-20

3) Post with two custom fields :

Data_START 2018-02-20

Data_END 2018-02-22

And so

---> no data

---> only starty data : 2018-02-20

--- start and end data : 2018-02-20 2018-02-22

I need to show for first all valid posts and rest (all expired and without data posts)

  • Why don't you sort only by Data_END? This would satisfy both your conditions. – sandrodz Feb 18 '18 at 8:14
  • Because some post have Data_END and other have only Data_START (I am not define Data_END if Event have only 1 day) – webstackoverload Feb 18 '18 at 8:37
  • You should provide example data and expected result of that data. Otherwise its unclear what your trying to achieve. – sandrodz Feb 18 '18 at 8:47
  • Topic has updated – webstackoverload Feb 18 '18 at 9:04
  • I don't think you can use your custom field's date the same way you use date_query. I assume meta_query would simply compare the strings, rather than being smart and comparing the date. – Jack Johansson Feb 18 '18 at 18:03
0

Here is the Code

function university_adjust_queries( $query ) {

    if ( ! is_admin() && is_post_type_archive( 'program' ) && $query->is_main_query() ) {
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' );
        $query->set( 'posts_per_page', -1 );
    }

    if ( ! is_admin() && is_post_type_archive( 'event' ) && $query->is_main_query() ) {
        $today = date( 'Ymd' );
        $query->set( 'meta_key', 'event_date' );
        $query->set( 'orderby', 'meta_value_num' );
        $query->set( 'order', 'ASC' );
        $query->set( 'meta_query', array(
            array(
                'key'     => 'event_date',
                'compare' => '>=',
                'value'   => $today,
                'type'    => 'numeric',
            )
        ) );
    }
}
add_action( 'pre_get_posts', 'university_adjust_queries' );
  • Not sure where did you get two CPTs! And seems you didn't answer his question. Could you please explain how this code solves his problem? – obiPlabon Feb 18 '18 at 12:07

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.