hanks bcworkz,
My code in my update_beginning_balance function looks like this
function update_bbal_ajax(){
add_filter( 'posts_where', 'filter_where' );
$recent = new WP_Query(array('post_type'=>'product','posts_per_page'=>-1,'post_status'=>'publish'));
remove_filter( 'posts_where', 'filter_where' );
while($recent->have_posts()) : $recent->the_post();
$postid = get_the_ID();
$new_val = 12;
$count = $recent->post_count;
echo the_title() . ' new beginning balance is ' . $new_val;
echo '<br/>';
update_post_meta( $postid, 'product_meta_beginning_balance', $new_val );
endwhile;
if($count == 1){
echo '<p>Successful!</p>';
echo 'A total of <strong>' . $count . '</strong> product updated!';
}
elseif($count >= 2){
echo '<p>Successful!</p>';
echo 'A total of <strong>' . $count . '</strong> products updated!';
}
else{
echo '<p>Failed!</p>';
echo 'No product updated!';
}
echo $_POST['start'];
}
add_action('wp_ajax_update_beginning_balance', 'update_bbal_ajax');
And I also have this filter_where function
‘function filter_where( $where = ” ) {
if(isset($_POST[‘start’]) && !empty($_POST[‘start’])){
$start = $_POST[‘start’];
}
if(isset($_POST[‘end’]) && !empty($_POST[‘end’])){
$start = $_POST[‘end’];
}
$where .= ” AND post_date >= ‘”.date(“Y-m-d”,$start).”‘ AND post_date <= ‘”.date(“Y-m-d”,$end).”‘”;
return $where;
}
add_action(‘wp_ajax_process_date’, ‘filter_where’);’
Now on this PHP code first, is it correct that I added action hook on my filter_where function? This function is what I am using to filter the result of my first function which is update_bbal_ajax. Then to add that additional where condition on my first function I added an add_filter hook on it.
On my jQuery I have added this
‘$(‘#update-form’).submit(function(){
data = {
action:’update_beginning_balance’,
formdata: $(‘#update-form’).serialize()
};
$.post(ajaxurl, data, function(updatebbal) {
$(‘#updated-bbal-result’).html(updatebbal);
})
return false;
});
});’
But the value on my date input aren’t passed. Also I have this jQuery
‘ jQuery(‘#start’).on(‘blur’, function(){
var date_value = this.value;
var ajaxdata = {
action: ‘process_date’,
date_value: date_value
};
jQuery.post(ajaxurl, ajaxdata, function(res){
// $(‘#date-selected’).html(res);
});
return false;
});
jQuery(‘#end’).on(‘blur’, function(){
var date_value = this.value;
var ajaxdata = {
action: ‘process_date’,
date_value: date_value
};
jQuery.post(ajaxurl, ajaxdata, function(res){
//$(‘#date-selected’).html(res);
});
return false;
});
‘
Which if I add some console.log I can see the posted date but still it doesn’t work because I couldn’t filter my result to the date range, rather it affects all the published products.
I really appreciate your help, thank you.