WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Accessing posts in the trash from PHP (5 posts)

  1. rjshuttleworth
    Member
    Posted 4 years ago #

    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

  2. MichaelH
    Member
    Posted 4 years ago #

    <?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().
    ?>
  3. rjshuttleworth
    Member
    Posted 4 years ago #

    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

  4. rjshuttleworth
    Member
    Posted 4 years ago #

    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

  5. MichaelH
    Member
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags