WordPress.org

Ready to get started?Download WordPress

Forums

WP_Query returning wrong result (2 posts)

  1. bibigeon
    Member
    Posted 4 months ago #

    My plugin adds a custom post type to wordpress, and id like to display all posts that have been published. I use wp_query to display all posts but it seems to fetch all post regardless of their status. Have I forgotten something ?

    Note that status changes from publish to draft, I use wp_update_post() to edit the post status . here is my code :

    Loop :

    function jobs_list_loop() {
        global $paged;
        //$curpage = $paged ? $paged : 1;
        if ( get_query_var('paged') ) {
                            $paged = get_query_var('paged');
                    } elseif ( get_query_var('page') ) {
                            $paged = get_query_var('page');
                    } else {
                            $paged = 1;
                    }
        $args = array(
            'post_type' => 'job',
            'orderby' => 'ID',
            'posts_per_page' => POST_PER_PAGE,
            'post_status' => 'publish',
            'paged' => $paged
        );
        $query = new WP_Query($args);
        $curpage = $paged;
        if($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
        ?>
        <div id="post-<?php the_ID(); ?>" class="quote">
        <?php
        echo get_the_post_thumbnail($post->ID, array($image_width,$image_height));
        the_content();
        ?>
        </div>
        <?php
        endwhile;
            echo '
            <div id="wp_pagination">
                <a href="'.get_pagenum_link(1).'">«</a>
                <a> 0 ? $curpage-1 : 1)).'">‹</a>';
                for($i=1;$i<=$query->max_num_pages;$i++)
                    echo '<a href="'.get_pagenum_link($i).'">'.$i.'</a>';
                echo '
                <a>max_num_pages ? $curpage+1 : $query->max_num_pages)).'">›</a>
                <a>max_num_pages).'">»</a>
            </div>
            ';
            wp_reset_postdata();
        endif;
    }

    Here is the code used to change status from publish to draft :

    $jb_post = array(
          'ID' => $id,
          'post_status' => 'draft'
      );
      wp_update_post( $my_post );

    Note that if I change to query to fetch only drafts, or trash or whatever it will do it successfuly. Am i forgetting something ? I have made tons of research and can't find anything giving a suitable answer.

    Thank you.

  2. bibigeon
    Member
    Posted 4 months ago #

    wp_update_post( $my_post ); is maent to be wp_update_post( $jb_post );, I made a mistake while editing teh code straight from teh text box...

    It obviously makes no difference, I still got the same issue

Reply

You must log in to post.

About this Topic