2017-12-04 17 views
0

私はWordPressで会社イントラネットを構築しています。私は(今の日から次の30日間の)今後の誕生日リストを取得しようとしています、誕生日の価値は、Ymd形式のカスタムフィールドから来ています。月と日付を基にした投稿の投稿

このコードは、すべての誕生日を返している:私は1ヶ月の期間のための唯一の誕生日を取得したい

$args = array(
      'post_type' => 'employees', 
       'meta_key' => 'birthday', 
       'orderby' => 'meta_value_num', 
       'order'  => 'ASC', 
       'compare' => '>=', 
       'type' => 'DATE', 
      ); 

+0

現在の月または次の30日間をお探しですか? – happymacarts

+1

何を試しましたか?あなたのコードを提供してください。これは単なる配列です。 – pr1nc3

答えて

0

必要な日付までクエリを実行する必要があります。あなたのクエリで特定の日付範囲を取得するために$start_date$end_dateを使用することができます

$start_date = date('Y-m-d', strtotime('- 1 month')); // or $start_date = date('Y-m-d'); 
$end_date = date('Y-m-d', strtotime('+ 1 month')); // or '+ 30 days' for example 

:だからあなたのような何かを行うことができます。

dateとAND/OR strtotimeを使用したときは、正しい場合は2038年のどこかにしか行くことができません。 2038年以降の日付をご希望の場合は、new DateTime機能を使用する必要があります。 (http://php.net/manual/en/datetime.construct.php

+0

私は今月の解決策を見つけました。 – Samarkandiy

+0

ニースが見つかりました! :)。私の答えがあなたの質問の答えでなかったら、あなた自身の答えを加えて、2日以内にそれを答えとすることができます。 –

0

私は今月の解決策を見つけました。

   $current_month = date('m'); 
       $filter_month = $current_month; // show current month only 

       $args = array (
        'post_type'  => 'employees', // your custom post type 
        'meta_key'  => 'birthday', // your custom date field name 
        'orderby'  => 'meta_value_num', 
        'order'   => 'ASC', 
        'meta_query' => array(
        array(
         'key'  => 'birthday', 
         'compare' => 'REGEXP', 
         'value' => '[0-9]{4}' . $filter_month . '[0-9]{2}', 
        ),  
       ) 
       ); 

       $posts = get_posts($args); 
関連する問題