• Resolved dreyz

    (@dreyz)


    wp_set_object_terms – does not work when run by cron

    I use cpt onomy in php script to import posts from xml to my blog.
    If I run import script in browser, all works fine, but if I run it by cron cpt-taxonomy not assigned to post. It seems like wp_set_object_terms function does not work

    cron
    /usr/local/bin/php /home/user/site.com/public_html/import/test-cpt-onomy.php

    For example simplified version of my script for debugging:

    <?php
    require_once('/home/user/site.com/public_html/wp-load.php');
    global $cpt_onomy;
    $cpt_onomy->wp_set_object_terms(11153, 9737, 'offer', false);
    echo "done!";
    ?>

    How to fix it?
    Thanks

    http://ww.wp.xz.cn/plugins/cpt-onomies/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Interesting. Let me do some research and I’ll get back to you. Thanks!

    Thread Starter dreyz

    (@dreyz)

    Rachel, I tested it and found that the function does not work when I’m not logged in wp admin section. And when run by cron is also not authorized.
    Maybe somewhere inside the function is checking whether the user is authorized, session or cookies?

    Because when I logged in and run the script in the browser (manually in the browser not by cron) it works fine!

    I hope this will help you find the problem.
    Thanks

    Ok. I think I figured it out. current_user_can() doesn’t work in CRON because it can’t detect the user which causes the function to fail so I added a DOING_CRON check.

    Open your cpt-onomy.php file, find the wp_set_object_terms() function and replace the following code:

    if ( ! current_user_can( $tax->cap->assign_terms ) )
       return new WP_Error( $tax->cap->assign_terms, __( 'You are not allowed to assign terms for this taxonomy.', CPT_ONOMIES_TEXTDOMAIN ) );

    with

    if ( ! defined( 'DOING_CRON' ) && ! current_user_can( $tax->cap->assign_terms ) )
       return new WP_Error( $tax->cap->assign_terms, __( 'You are not allowed to assign terms for this taxonomy.', CPT_ONOMIES_TEXTDOMAIN ) );

    Did that do the trick?

    Thread Starter dreyz

    (@dreyz)

    Thank you, Rachel!
    Now it worked with this fix! 🙂

    Your plugin really helped me with my site!
    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘wp_set_object_terms – does not work when run by cron’ is closed to new replies.