admin管理员组

文章数量:1122846

I have a script that works as a standalone php file, but if I put it in a Wordpress hook action with a function, the checkposts() function stops working. I tried to put the checkposts function outside of the hook function but that did not seem to work. any ideas?:

add_action(  'populate_inventory_hook',  'populate_inventory' );
function populate_inventory() {
$url = '';

//  Initiate curl
$ch = curl_init();
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);

$items = json_decode($result, true);
$feed_count = count($items);

 
   
function checkposts($items,$postid) {
    global $post;   
    $wp_vin = get_post_meta( $postid, "item_vin", true);        
    $i = 0;
     foreach ($items as $item) {   
         if ($item['vin'] ==  $wp_vin) {
            $i++;
         }       
     }     
     if ($i < 1) {
        wp_delete_post($postid);         
     }     
     return $i; 
}   

$check_counter = 0;
$exist_counter = 0;
$non_exist_counter = 0;

$wp_args = array(  
        'post_type' => 'wheelchair-vans-sale',
        'posts_per_page' => -1, 
        'post_status' => 'publish',
    );

$query = new WP_Query( $wp_args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); 

$check_counter++;

if ( checkposts($items,$post->ID) ) {
    echo "1";
    $exist_counter++;     
} else {
     echo "0";
    $non_exist_counter++;
}

endwhile; 
echo "<br />";
 wp_reset_postdata();
 
 endif; 
}

I have a script that works as a standalone php file, but if I put it in a Wordpress hook action with a function, the checkposts() function stops working. I tried to put the checkposts function outside of the hook function but that did not seem to work. any ideas?:

add_action(  'populate_inventory_hook',  'populate_inventory' );
function populate_inventory() {
$url = 'https://website.com/webfeed?version=2';

//  Initiate curl
$ch = curl_init();
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);

$items = json_decode($result, true);
$feed_count = count($items);

 
   
function checkposts($items,$postid) {
    global $post;   
    $wp_vin = get_post_meta( $postid, "item_vin", true);        
    $i = 0;
     foreach ($items as $item) {   
         if ($item['vin'] ==  $wp_vin) {
            $i++;
         }       
     }     
     if ($i < 1) {
        wp_delete_post($postid);         
     }     
     return $i; 
}   

$check_counter = 0;
$exist_counter = 0;
$non_exist_counter = 0;

$wp_args = array(  
        'post_type' => 'wheelchair-vans-sale',
        'posts_per_page' => -1, 
        'post_status' => 'publish',
    );

$query = new WP_Query( $wp_args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); 

$check_counter++;

if ( checkposts($items,$post->ID) ) {
    echo "1";
    $exist_counter++;     
} else {
     echo "0";
    $non_exist_counter++;
}

endwhile; 
echo "<br />";
 wp_reset_postdata();
 
 endif; 
}
Share Improve this question asked Jun 13, 2024 at 3:23 PeterPeter 1531 silver badge8 bronze badges 2
  • 2 I haven't seen the hook populate_inventory_hook. I'd guess that it's firing too early though so you don't have access the global $post yet when it is called. You say it "stops working"...What error are you getting? Did you check logs? Try running your plugin on a hook like template_redirect and see if you get anything. Otherwise let us know what the error logs say. – rudtek Commented Jun 13, 2024 at 3:44
  • Hi rudtek, thank you for the response. So, the script was originally a cronjob, but the hosting does not have a control panel to add cronjobs, so I am trying to use the plugin called WP Crontrol, which requires you to put the code that you want to run inside of a hook, so I created the populate_inventory_hook , as I only want it to run when the cronjob is activated. I need to try to find the error, but when I say it is not working, it does not find any posts in the array like it should. Thank you again. – Peter Commented Jun 13, 2024 at 15:47
Add a comment  | 

1 Answer 1

Reset to default 0

To ensure that the checkposts function works properly within a WordPress hook action, you can define the checkposts function outside the populate_inventory function. This approach keeps the code organized and ensures that the function is available when needed. Here's the revised version of your code:

add_action( 'populate_inventory_hook', 'populate_inventory' );

function populate_inventory() {
    $url = 'https://website.com/webfeed?version=2';

    //  Initiate curl
    $ch = curl_init();
    // Will return the response, if false it print the response
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    // Set the url
    curl_setopt( $ch, CURLOPT_URL, $url );
    // Execute
    $result = curl_exec( $ch );
    // Closing
    curl_close( $ch );

    $items = json_decode( $result, true );
    $feed_count = count( $items );

    $check_counter = 0;
    $exist_counter = 0;
    $non_exist_counter = 0;

    $wp_args = array(
        'post_type'      => 'wheelchair-vans-sale',
        'posts_per_page' => -1,
        'post_status'    => 'publish',
    );

    $query = new WP_Query( $wp_args );
    if ( $query->have_posts() ) : 
        while ( $query->have_posts() ) : 
            $query->the_post(); 

            $check_counter++;

            if ( checkposts( $items, $post->ID ) ) {
                echo "1";
                $exist_counter++;     
            } else {
                echo "0";
                $non_exist_counter++;
            }

        endwhile; 
        echo "<br />";
        wp_reset_postdata();
    endif; 
}

function checkposts( $items, $postid ) {
    global $post;   
    $wp_vin = get_post_meta( $postid, "item_vin", true );        
    $i = 0;
    foreach ( $items as $item ) {   
        if ( $item['vin'] ==  $wp_vin ) {
            $i++;
        }       
    }     
    if ( $i < 1 ) {
        wp_delete_post( $postid );         
    }     
    return $i; 
}

In this setup, the checkposts function is defined separately and is called within the populate_inventory function to perform the necessary checks. This structure ensures that all functions are properly defined and accessible when the hook is executed.

本文标签: How to use a function inside of a functionor rewrite this code to work