Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to perform some checks on custom fields before they are saved on a db. So I decided to use an add_filter as it does this between browser and db. Here is my code

function fields_pre_check($post_id){

$errors = false;
$values = get_post_custom(get_the_ID());
$act_val = $_POST['checks_news'];

...altri checks...

if(get_check_key($post_id, $act_val , $values)){            

    $errors = true;
    update_option('custom_token', $errors);
    update_option('custom_admin_errors', $txt_err=error_text($errors, 2));

    remove_action('save_post','update_fields');
    wp_update_post(array('ID' => get_the_ID(), 'post_status' => 'draft'));
    add_action('save_post', 'update_fields');

    return false;
}
}
function update_fields($post_id){
$chk1 = $_POST['checks_news'];
update_post_meta($post_id, 'checks_news', $chk1);
}
function get_check_key($post, $checker, $val){
global $wpdb;
$wpdb->query("SELECT meta_value, meta_key
             FROM $wpdb->wp_postmeta
             WHERE meta_value = $checker");
if(get_post(get_the_ID())==NULL){

    return ($wpdb->num_rows != 0);
}
else{
    if($val['checks_news'][0] == $checker)
        return false;
    else{
        return ($wpdb->num_rows != 0);
    }
}
}
add_filter('wp_insert_post_data', 'fields_pre_check');
add_action('save_post', 'update_fields');

I choose add_filter because the function get_check_key has to verify the previous value $val['checks_news'][0] with the new one $checker( if I use add_action, which i tried, they are the same because $val['checks_news'][0] retrieves the value just saved so if wanna change that value from 1 to 2 and perform the check it returns 2 == 2). The problem comes when I want to use $wpdb->num_rows which returns always 0 even if the result should be 1. What am i doing wrong? Did I take the right way using add_filter instead of add_action?

share|improve this question

1 Answer 1

A few things I would do/add/change:

Instead of using a query function directly I would use $wpdb->get_results

Second, $wpdb->num_rows will work now.

Third: setup_postdata() is what you need to actually make use of the query outside of a loop. This means get_post(get_the_ID()) doesn't apply to anything as there is no existing $post-ID to reference.

I understand what you are trying to do but you will need several more lines of code to get it done correctly. It seems a somewhat advanced function for someone who isn't ready to write it.

In other words - you need to make sure your objects, arrays, and variables are properly set before you can effectively make use of them within the wpdb and/or wordpress class functions.

share|improve this answer
    
I changed get_post with get_results, but the error wasn't in this: my string query was not complete, in fact the variable $checker at the end was surrounded by the back quotes and this doesn't work, when I changed them with the normal ones everything was fine. Then i used get_the_ID because I need to perform the check on the current real post and not the revision just saved which $post->ID returns its ID for. And I also changed the add_filter with add_action('save_post'...) which aparently works very well and it perfectly does what I need. I hope that's the correct way! –  Tony Feb 27 '12 at 8:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.