仅删除查询内的POST/FOR每个语句(前端)

时间:2017-03-08 作者:joelybristol

我在我的函数中使用它,我可以使用它删除当前页面(称为-bucket的自定义帖子类型)及其所有子页面:

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;
}
}
然后我把它称为我的主题

<?php echo delete_post(); ?>
当我在显示所有帖子的页面模板中使用此函数时,它显然会删除整个页面及其所有子页面,而不仅仅是通过查询/for each语句调用单个帖子

下面是我正在使用的示例模板代码以及调用我的函数的位置

<?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(); ?>
因此,您可以在上面的模板中的span class-modify文件夹中看到我调用的删除链接。但是它删除了整个页面,而不仅仅是$section->ID-我已经尝试将所有引用更改为将主功能文件中的\\u ID和post\\u ID改为$section->ID,但它不起作用!

请帮忙!

谢谢

1 个回复
最合适的回答,由SO网友:Industrial Themes 整理而成

您应该将要删除的帖子的id作为delete\\u post()函数的参数传递,而不是依赖于通过函数本身中的global$post获取它:

<?php echo delete_post($section->ID); ?>
然后,在delete\\u post()函数中,可以使用传入的参数,如下所示:

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>\';
    }
}

相关推荐

无法在WordPress中通过PHP显示图像

我有一个奇怪的问题,当我尝试使用PHP输入图像标记的源代码时,它会在检查器中显示以下错误<img src=(unknown) alt=\"\"> 这个代码片段为我提供了正确的url,通过查看CPanel并粘贴和复制地址进行检查,但当我尝试通过php输入它时,不会显示图像$image = wp_get_attachment_image_src( $post_thumbnail_id); //echo($image[0]); ?> <img src=