• Resolved Mort

    (@rugbert)


    So Ive almost completed this project. Im using custom fields, and a custom search (wpdb) to deal with my posts.

    My problem, and this is huge, is that when I go to manage my posts and hit trash, they still exist in the DB.

    So my custom search and the way I display my posts (modified search, Im filtering based on a custom field and putting the results in tables) doesnt work because the stuff I trash STILL shows up :/

    here is my code:

    for($t=1; $t<=2; $t++){
    $postids=$wpdb->get_col($wpdb->prepare("
    SELECT DISTINCT post_id
    FROM   wp_postmeta
    WHERE  post_id IN (SELECT post_id
    	   FROM   wp_postmeta
    	   WHERE  ( meta_key = 'Bedrooms'
    				AND meta_value = '$t' ))
    AND post_id IN (SELECT post_id
    		   FROM   wp_postmeta
    		   WHERE  ( meta_key = 'Active'
    					AND meta_value = 'Yes' ))
    AND post_id IN (SELECT post_id
    		   FROM   wp_postmeta
    		   WHERE  ( meta_key = 'listType'
    					AND meta_value = 'Rental' ))
    ",$t)); ?>
    <table class="myTable">
    <thead>
    <tr>
    <th><?php echo($t.' Bedrooms'); ?></th>
    <th>Price</th>
    <th>Available</th>
    </tr>
    </thead>
    <?php
    $tr = 0;
    if (!empty($postids)) {
    foreach ($postids as $id) {
    $tr++;
    if($tr%2){$trClass="dark";}else{$trClass="light";}
    $post=get_post(intval($id));
    setup_postdata($post);
    $utilities = get('Utilities');
    ?>
    <tr class="<?php echo ($trClass); ?>">
    <td class="address"><a href="<?php the_permalink(); ?>"><?php
    echo get('Address');
    ?></a>
    </td>
    <td class="price"><a href="<?php the_permalink(); ?>"><?php
    echo ('$'. get('Price'));
    ?></td></a>
    <td class="date"><a href="<?php the_permalink(); ?>"><?php
    echo get('Availability');
    ?></td></a>
    </tr>
    <?php
    }
    } wp_reset_query();
    ?>
    </table>
    
    <?php
    }
    ?>

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Mort

    (@rugbert)

    I guess this part of the loop:

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

    is what properly pulls active posts from the db? I just used the standard loop and my the post I deleted didnt appear. But as soon as I used my custom search again its there.

    HELP!

    Moderator James Huff

    (@macmanx)

    Volunteer Moderator

    Trash is actually a holding status. The post is removed from view, but it remains in the database for 30 days unless removed manually. When viewing the list of posts in the admin panel, click the “Trash” link near the top and either select “Delete Permanently” underneath the post or hit the “Empty Trash” button.

    http://codex.wordpress.org/Trash_status

    It looks like your are basing what posts you want to pull off of the posts meta. You will probably want to INNER JOIN the wp_posts table against the wp_postmeta so you can filter with a WHERE clause as post_status = 'publish' select only postmeta from what are published posts.

    Something like this. (Not tested)

    SELECT DISTINCT wp_postmeta.post_id
    FROM wp_postmeta
    INNER JOIN wp_posts
    ON wp_postmeta.post_id=wp_posts.post_id
    WHERE wp_postmeta.post_id IN (SELECT post_id
    	   FROM   wp_postmeta
    	   WHERE  ( meta_key = 'Bedrooms'
    				AND meta_value = '$t' ))
    AND wp_postmeta.post_id IN (SELECT post_id
    		   FROM   wp_postmeta
    		   WHERE  ( meta_key = 'Active'
    					AND meta_value = 'Yes' ))
    AND wp_postmeta.post_id IN (SELECT post_id
    		   FROM   wp_postmeta
    		   WHERE  ( meta_key = 'listType'
    					AND meta_value = 'Rental' ))
    AND wp_posts.post_status = 'publish'

    Thread Starter Mort

    (@rugbert)

    it actually kills the query :/ Im not very good at SQL yet but I love your solution, can you help me with it?

    Thread Starter Mort

    (@rugbert)

    nvm, I got it. wp_posts doesnt have a column called post_id, its called ID

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Trashing Posts does not Remove from Database’ is closed to new replies.