Link to home
Start Free TrialLog in
Avatar of Alicia St Rose
Alicia St RoseFlag for United States of America

asked on

When I draft post from a list on frontend, all posts on the page get drafted

Okay, I'm creating a web app in WordPress and I'm trying to allow users to draft a post from the front end. The posts are custom post types called "lessons". I have a dashboard that lists all the lessons the user has created and I have a deactivate checkbox and link under each lesson. I got the drafting functionality to work, however, when I draft only ONE lesson, they all get drafted.

Here's my code:

In the Template:

<span class="deactivate-link"><form action="" method="POST" >
			<input type="checkbox" value="deactivate" name="draft"> <label for="deactivate">Make inactive</label>
			<input type="submit" value="Deactivate">
		</form>
	<?php toDraft($post->ID); ?>
		</span>

Open in new window


In the functions.php:

//function to update post status to draft
function toDraft($pid){

$toDraft = $_POST['draft'];
   if($toDraft == 'deactivate'){
      wp_update_post(array('ID' => $pid, 'post_status'   =>  'draft'));
  }
}

Open in new window


User generated image
Avatar of David Favor
David Favor
Flag of United States of America image

Maybe add some debug code to show the post ID in your lesson title.

Seems like maybe you have duplicate IDs in your database.

Also, enable database general logging + watch all database i/o which generates when you click deactivate, as this might also show multiple SQL UPDATEs rather than one UPDATE.
Avatar of Alicia St Rose

ASKER

Hi David,
It's been a while since I've gotten a chance to return here. Unfortunately, I can't really understand what you're trying to tell me to do.

1. I don't know how or what to add as debug code

2. Don't know how to enable database general logging or what "i/o" is defined as.

Not totally green over here, but learning as I go!
I'm coming back to this. What do I have to provide so I can move forward. I'm sure it's an Ajax problem and I'm really green at Ajax at the moment.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.