WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Accessing posts in the trash from PHP

[Resolved] Accessing posts in the trash from PHP

  • Hi all, (my first post)

    I am trying to access posts from PHP that are in the trash. I know I can return content of a trashed post when I know the ID of the post, as I can use:

    $post = get_post($id);
    echo $post->post_title;

    after issuing a: wp_delete_post($id);

    but I’d like to be able to produce a list of trashed items for use in my site. Can anyone point me in the right direction? Ideally there’ll be some metadata that I can pull that I don’t know about yet, and that my google searches are failing to uncover! I’m effectively trying to select all posts in the trash for a particular author.

    Thanks all!
    R

Viewing 4 replies - 1 through 4 (of 4 total)
  • <?php
    $author_id = 1;
    $args=array(
      'author' => $author_id,
      'post_type' => 'post',
      'post_status' => 'trash',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Trash Posts for author id 1';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Thanks Michael – i’ll give that a whirl. Great to have such a responsive community. A real help when starting out on WordPress! Thanks again, R

    Hi Michael – i confirm that this code above works and gives me the list – thanks. I set the post to resolved for that reason.

    One thing though – maybe a separate topic. The permalink function returns URLs that are not accessible. Appears that once items are in the trash you can retrieve info about them programmatically but cannot link to them in the normal way.

    R

    And that makes sense doesn’t it? It’s trash.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] Accessing posts in the trash from PHP’ is closed to new replies.
Skip to toolbar