0

I am trying to group( archive ) my CPT (fcra_donation : has_archive => true) based on a custom date Field ( sukritham-donation-date : format yyyymmdd (20160506).

Here is my code in my functions.php

function fcra_rewrite(){

add_rewrite_rule(
'^fcra_donation/([0-9]{4})/?$',
'index.php?post_type=fcra_donation&donation_year=$matches[1]',
'top'
);

}

add_action( 'init', 'fcra_rewrite' );

//Add donation_year to query_Vars
function fcra_query_vars($vars) {
$vars[] = 'donation_year';
return $vars;
}

add_filter('query_vars', 'fcra_query_vars');

function fcra_donation_query( $query ) {
if(! $query->is_post_type_archive( 'fcra_donation' ))
    return;

if($query->is_main_query() && ! is_admin()) {
    ( !empty($query->query_vars['donation_year']) ) ? $query_year = $query->query_vars['donation_year'] : $query_year = date( 'Y' );
    $date_start = $query_year.'01'.'01';
    $date_end = $query_year.'12'.'31';
    d($date_start, $date_end);

    $meta_query = array (
        array(
            'key' => 'sukritham-fcra-date',
            'value' => array( $date_start, $date_end ),
            'type' => 'DATE',
            'compare' => 'BETWEEN',
        )
    );

    //modify query

    $query->set( 'meta_key', 'sukritham-fcra-date' );
    $query->set( 'orderby', 'meta_value' );
    $query->set( 'order', 'ASC' );
    $query->set( 'meta_query', $meta_query );
}
}

add_action( 'pre_get_posts', 'fcra_donation_query' );

As such i can access fcra_donation/ and fcra_donation/2017 and it is working and giving me the correct lists . But for fcra_donation/2016 or fcra_donation/2015 it is returning page not found . If i hard code $date_start and $date_end with 2015 or 2016 it will work . The query_vars 'donation_year' is not found in $wp_query->query_vars . The 'Matched Rule' is shown as fcra_donation/([0-9]{4})/?$ and 'Matched Query' is post_type=fcra_donation&year=2017 . Can somebody guide me , Thanks in advance .

  • both http://localhost/ww/?post_type=fcra_donation&donation_year=2016 and http://localhost/ww/?post_type=fcra_donation&donation_year=2015 gives the desired results ....... – Anoop D Aug 8 '17 at 8:04
  • Milo Can you please help me regrding this ? – Anoop D Aug 9 '17 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.