if (!empty($year)) $query->set('year', $cDate['year']); if (!empty($monthnum)) $query->set('monthnum', $cDate['mon']); if (!empty($day)) $query->set('day', $cDate['mday']); return $query; } if (!empty($year) and ! empty($monthnum) and ! empty($day)) { $post_date = jalali_to_gregorian($year, $monthnum, $day); $query->set('year', $post_date[0]); $query->set('monthnum', $post_date[1]); $query->set('day', $post_date[2]); return $query; } if (!empty($year) and ! empty($monthnum)) { $start_date = jalali_to_gregorian($year, $monthnum, 1); $end_date = jalali_to_gregorian($year, $monthnum, jday_of_month($year, $monthnum)); $date_query = array( array( 'after' => array( 'year' => $start_date[0], 'month' => $start_date[1], 'day' => $start_date[2], ), 'before' => array( 'year' => $end_date[0], 'month' => $end_date[1], 'day' => $end_date[2], ), 'inclusive' => TRUE, ), ); $query->set('date_query', $date_query); $query->set('year', ''); $query->set('monthnum', ''); // $post_date = jalali_to_gregorian($year, $monthnum, 15); // $query->set('year', $post_date[0]); // $query->set('monthnum', $post_date[1]); return $query; } if (!empty($year)) { $start_date = jalali_to_gregorian($year, 1, 1); if (is_jalali_leap_year($year)) $end_date = jalali_to_gregorian($year, 12, 30); else $end_date = jalali_to_gregorian($year, 12, 29); $date_query = array( array( 'after' => array( 'year' => $start_date[0], 'month' => $start_date[1], 'day' => $start_date[2], ), 'before' => array( 'year' => $end_date[0], 'month' => $end_date[1], 'day' => $end_date[2], ), 'inclusive' => TRUE, ), ); $query->set('date_query', $date_query); $query->set('year', ''); return $query; } return $query; } /* =================================================================== */ /** * jalali link filter handlers * @param string $perma * @param string $post * @param string $leavename * @see wp-includes\link-template.php line 112 */ function ztjalali_permalink_filter_fn($perma, $post, $leavename = false) { $permalink = get_option('permalink_structure'); if (empty($permalink)) return $perma; if(!(preg_match('/%year%|%monthnum%|%day%/', $permalink))) return $perma; /* ------------------------------------------------------ */ if (empty($post->ID)) return $perma; /* ------------------------------------------------------ */ if (in_array($post->post_status, array('draft', 'pending', 'auto-draft'))) return $perma; /* ------------------------------------------------------ */ $illegal_post_types = get_post_types(array('_builtin' => false)); $illegal_post_types[] ='page'; $illegal_post_types[] ='attachment'; if (in_array($post->post_type,$illegal_post_types)) return $perma; /* ------------------------------------------------------ */ $rewritecode = array( '%year%', '%monthnum%', '%day%', '%hour%', '%minute%', '%second%', $leavename ? '' : '%postname%', '%post_id%', '%category%', '%author%', $leavename ? '' : '%pagename%', ); $unixtime = strtotime($post->post_date); $category = ""; if (strpos($permalink, '%category%') !== false) { $cats = get_the_category($post->ID); if ($cats) { usort($cats, '_usort_terms_by_ID'); // order by ID $category_object = get_term(reset($cats), 'category'); $category = $category_object->slug; if ($parent = $category_object->parent) $category = get_category_parents($parent, false, '/', true) . $category; } if (empty($category)) { $default_category = get_term(get_option('default_category'), 'category'); $category = is_wp_error($default_category) ? '' : $default_category->slug; } } $author = ""; if (strpos($permalink, '%author%') !== false) { $authordata = get_user_by('id', $post->post_author); if(!empty($authordata)) $author = $authordata->user_nicename; } $date = explode("-", date('Y-m-d-H-i-s', $unixtime)); $jdate = gregorian_to_jalali($date[0], $date[1], $date[2]); $rewritereplace = array( $jdate[0], sprintf('%02d', $jdate[1]), // add leading zero if necessary sprintf('%02d', $jdate[2]), $date[3], $date[4], $date[5], $post->post_name, $post->ID, $category, $author, $post->post_name, ); $permalink = home_url(str_replace($rewritecode, $rewritereplace, $permalink)); return user_trailingslashit($permalink, 'single'); } /* =================================================================== */