0

Specifically WooCommerce Product Pages.

I'm using WP_Query to generate the array.

$params = array(
    'posts_per_page' => -1,
    'post_type' => 'product',
    'orderby' => 'menu-order',
    'order' => 'asc',
    'fields' => 'ids',
    'tax_query' =>
    array(
        array(
            'taxonomy' => 'product_visibility',
            'field' => 'term_id',
            'terms' => 7,
            'operator' => 'IN'
            )
        ));
$wc_query = new WP_Query($params);
$ids = $wc_query->posts;

the array in $ids looks like

Array
(
    [0] => 8700
    [1] => 2558
    [2] => 2579
    [3] => 2582
    [4] => 2588
    . . . . . .

I've tried both a for loop

for ($i = 0; $i < $postcount; $i++) {
    wp_update_post(array('ID' => $ids[$i], 'post_status'   =>  'draft'));
}

and a foreach loop

foreach($post_ids as $post_id) {
     $post = array( 'ID' => $ids, 'post_status' => 'draft' );
     wp_update_post($post);
}

Neither are working. The pages are still set to Published.

Anyone have any idea why this isn't working?

0

Well in your for loop, I think you're missing the $postcount variable... also there's a typo at $idss[$i] .... should be $ids[i] based on what you've shown.

for ($i = 0; $i < $postcount; $i++) {
    wp_update_post(array('ID' => $ids[$i], 'post_status'   =>  'draft'));
}

That being said I'd just go with a foreach loop.

The mistake that you're making is the 2nd line.

<?php 
  $post_ids = $wc_query->posts;

  foreach($post_ids as $post => $post_id) {
    echo $post_id;
  }
?>

Assuming that results in a nice list of ID's, then I'd do the following:

 <?php 
  $post_ids = $wc_query->posts;

  foreach($post_ids as $post => $post_id) {
    echo $post_id;
    $arrPost = array( 'ID' => $post_id, 'post_status' => 'draft' );
    wp_update_post($arrPost);
  }
?>

I'm afraid this is the best I can do without being able to access the files.

Remember var_dump() is your friend!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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