从自定义帖子类型下拉列表中选择的发布日期到ajax

时间:2013-12-03 13:59:09

标签: php jquery ajax wordpress

我有一个自定义帖子类型的日期下拉列表。所以发布帖子的所有日期都显示..我需要将此信息发送到我的ajax页面。我该怎么做呢 这是我获取帖子日期的代码

<select id="date">
<?php
$dates = array();
$argez = (array( 'post_type' => 'latest_message'));
query_posts( $argez );
  if (have_posts())  : while (have_posts()) : the_post(); 
    $dates[] = get_the_date();
    $dates = array_unique($dates);
    print_r($datesun);

   endwhile; 
   foreach($dates as $date) {
    echo '<option value="' . $date . '">' . $date .'</option> ';
  }
 endif;
?>
</select>

我还有另一个下拉菜单,用于获取分类标题主题信息

<?php 
//list terms in a given taxonomy using wp_list_categories (also useful as a widget if using a PHP Code plugin)

$taxonomy     = 'topic';
$orderby      = 'name'; 
$show_count   = 0;      // 1 for yes, 0 for no
$pad_counts   = 0;      // 1 for yes, 0 for no
$hierarchical = 1;      // 1 for yes, 0 for no
$title        = '';

$args_topic = array(
  'taxonomy'     => $taxonomy,
  'orderby'      => $orderby,
  'show_count'   => $show_count,
  'pad_counts'   => $pad_counts,
  'hierarchical' => $hierarchical,
  'title_li'     => $title,
   'show_option_all'     => 'TOPIC'
);
?>

<ul class="taxonomy-drops series-topic one-fifth">
<li id="categories">
    <form action="<?php bloginfo('url'); ?>" method="get">
    <div>
    <?php wp_dropdown_categories($args_topic); ?>
    </div>
    </form>
</li>
</ul>

我将这些信息发布到我的ajax页面,并且工作正常

 if ($_POST["topic"]!=0 ) {
$topic = array($_POST["topic"]);
} else {
     $topic = get_terms( 'topic', array('fields' => 'ids') );
     //$topic = array(implode(', ',$topic));

}

如何发布我的日期,以便当我点击日期时,它会从所选主题和相应日期中获取信息并给出结果。这是我激活我的ajax的功能

jQuery('. .series-topic #cat').on('change',function() {
    var selectedTopic = jQuery('.series-topic #cat').val();
    var selectedDate = jQuery('.series-date #date').val();
    console.log(selectedTopic);
    console.log(selectedDate);
    jQuery.ajax({
        type: 'post',
        url: '/ajax',
        data: {
            topic: selectedTopic,
            date: selectedDate
        },
         success:function(data) {
             if(data) {  
                 jQuery('.-hold').html(data);
            } else {}
        }
    });
});

如何使用此

了解我的约会对象

1 个答案:

答案 0 :(得分:0)

这应该适用于处理发布日期和根据日期条件查询帖子。 (注意:此代码未经测试,请原谅任何错别字)

if($_POST['date'] != null){
    $date_parsed = strtotime($_POST['date']);
    $args = array(
        'date_query' => array(
        array(
            'year'  => date('Y',$date_parsed),
            'month' => date('m',$date_parsed),
            'day'   => date('d',$date_parsed),
        ),
        ),
    );
    $query = new WP_Query( $args ); 

    while ($query->have_posts()){ 
         $query->the_post(); 
         //DO WHATEVER YOU WANT WITH EACH POST
    }
}

另外 - 我想你甚至会问你如何在日期下拉列表中附加更改。要做到这一点,请更改:

jQuery('. .series-topic #cat').on('change',function() {

要:

jQuery('. .series-topic #cat, #date').on('change',function() {