1

I am using this in my functions which I can use to delete the current page (custom post type called - bucket) and all it's children:

function delete_post(){
global $post;
$deletepostlink= add_query_arg( 'frontend', 'true',   get_delete_post_link( get_the_ID() ) );
if (current_user_can('edit_post', $post->ID)) {
    echo '<span><a class="post-delete-link" onclick="return confirm(\'Are you sure to delete?\')" href="' . $deletepostlink . '">Delete  this </a></span>';
}
}

//Redirect after delete post in frontend
add_action('trashed_post','trash_redirection_frontend');
function trash_redirection_frontend($post_id) {
if ( filter_input( INPUT_GET, 'frontend', FILTER_VALIDATE_BOOLEAN ) ) {
    $args = array(
        'posts_per_page' => -1,
        'order'=> 'ASC',
        'post_parent' => $post_id,
        'post_type' => 'bucket'
    );
    // Filter through all pages and find Portfolio's children
    $children = get_children( $args );
    global $wpdb;
    foreach($children as $child){
        $childs[$child->ID] = $child->ID;
    }
    $sql = "UPDATE {$wpdb->posts} SET post_status = 'trash' WHERE ID IN   (" . implode( ', ', $childs ) . ")";
    $wpdb->query($sql);
wp_redirect();
    exit;
}
}

I then call this into my theme with this

<?php echo delete_post(); ?>

When I use this function within my page template displaying all my posts it obviously deletes the whole page and all it's children instead of just the individual post being called with a query / for each statement

Here is the example template code I am using and where my function is being called

<?php 
/*
Template Name Posts: Bucket Overview
*/
?>



<?php get_header(); ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>


<div class="content">
<div class="stash-container">


<div class="main-view">

 <?php
 global $authordata, $post;
 $my_meta = get_post_meta( $id, 'connect', true);
 $portfolio_sections = array(
 'post_type'   => 'bucket',
 'category_name'      => 'Folder',
 'meta_key' => 'connect',
    'meta_value' => $my_meta,
 'orderby' => 'menu_order',
 'posts_per_page' => -1,
 'sort_column' => 'menu_order',
 'sort_order'  => 'ASC'
  );

  $sections = get_posts($portfolio_sections);
  foreach ($sections as $section) { ?>


   <ul class="things">

     <div class="category"> <i class="fa fa-folder-o" aria-   hidden="true"> </i> <?php echo $section->post_title; ?>
         (<?php $children = get_pages( array( 'child_of' => $section-  >ID, 'post_type'   => 'bucket',) ); echo count($children);?>)

  <span class="modify-folder">

  <?php echo delete_post(); ?> | edit

  </span>
  </div>


    <?php
    global $authordata, $post;
    $my_meta = get_post_meta( $id, 'connect', true);
    $portfolio_sites = array(
    'post_type'   => 'bucket',
    'category_name' => 'drop',
    'meta_key' => 'connect',
    'meta_value' => $my_meta,
    'orderby' => 'menu_order',
    'posts_per_page' => -1,
    'sort_column' => 'menu_order',
    'sort_order'  => 'ASC',
     'post_parent'      => $section->ID
     );

      $sites = get_posts($portfolio_sites);


      foreach ($sites as $site) { ?>
      <li class="sites">

       <a href="<?php echo get_the_permalink($site->ID); ?>">

      <div class="rollover"> 

    </div>
    </a>
  </li>
  <?php } ?>
  </ul>
  <?php } ?> 
  <?php endwhile; endif; ?>
  <?php get_footer(); ?>

So you can see within the above template in the span class - modify-folder I am calling my delete link. But it is deleting the whole page not just the $section->ID - I have tried changing all references to get_the_id and post_id in the main function file to be $section->ID instead but it doesnt work!

Please help!

Thanks

  • Try changing $childs[$child->ID] = $child->ID; to $childs[] = $child->ID; - key indexed arrays are squiffy with implosion – boylett Mar 8 '17 at 19:01
1

You should pass in the id of the post you want to delete as an argument of the delete_post() function rather than relying on obtaining it via global $post within the function itself:

<?php echo delete_post($section->ID); ?>

Then in your delete_post() function you can use the passed-in argument like so:

function delete_post($sectionid){
    $deletepostlink= add_query_arg( 'frontend', 'true',   get_delete_post_link( $sectionid ) );
    if (current_user_can('edit_post', $sectionid)) {
        echo '<span><a class="post-delete-link" onclick="return confirm(\'Are you sure to delete?\')" href="' . $deletepostlink . '">Delete  this </a></span>';
    }
}
  • Hey thanks for this! Will try this out when I get home - so to clarify it's only - $sectionid for the passed in argument in the actual function and not $section->id? – joelybristol Mar 8 '17 at 16:04
  • That is correct. You could name it whatever you want once you're within the function, I just kept it close to the original so as to be descriptive. It would also make sense for it to be $postid or even just $id. But when you pass it in it has to be $section->ID – Industrial Themes Mar 8 '17 at 16:44
  • Great! So assume to ensure children get deleted too I set post parent in my after trash function to be $sectionid also? Thanks for your time - I'll make to mark as right in an hour or so when I give it a go! – joelybristol Mar 8 '17 at 16:54
  • just tried this out and it's not working! It's close it seems now delete the correct post along with its parent - instead of it's children which it should do! – joelybristol Mar 8 '17 at 17:53
  • Perhaps I'm not understanding what you're wanting to do then. I thought you wanted it to delete the section and it was not deleting the specific one? – Industrial Themes Mar 8 '17 at 20:31

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.