0

I created a custom post type tickets, than I added few tickets in it. But with the following code, I tried to show list of tickets submitted by the current user and its not working for me.

global $post, $paged, $current_user;
get_currentuserinfo();
if(empty($paged)) $paged = 1; 
    $args = array(
                'post_type' => 'tickets',
                'post_author' => $current_user->ID, 
                'paged'=> $paged
            );  
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :  setup_postdata($post);
            echo "test echo"; // testing purpose
            echo "<tr><td>".the_title()."</td><td>".the_excerpt()."</td><td>".$post->post_status."</td></tr>";
     endforeach; 
   wp_reset_postdata();

And its showing empty results in the table. I am not sure about similar questions. But this is my code. so please dont vote it down, if you think its not a right question. i need solution for my issues, i am breaking my head more than one hour.

Thank you for all of your comments, actually I found solution myself. Sometimes, we may not think a small thing can cause certain unexpected results. The problem is simple. the current user has few pending tickets. The important thing , here, if we didn't specify the post_status. It will assign default publish. So it didn't get results.

  • How are you getting the $current variable? Did you try echoing it out to see what is contains? you could also try removing the 'paged' parameter, just for testing. – Mateusz Hajdziony Nov 20 '15 at 4:30
  • i have edited my code with $current_user. but still its not working. – Kvvaradha Nov 20 '15 at 4:35
  • if i remove this 'post_type' => 'tickets',. it shows combined posts together. but its not filtering the tickets custom post type's posts. – Kvvaradha Nov 20 '15 at 4:39
  • A common mistake for me is trying to get post type 'tickets' when they have actually been registered as 'ticket'. Just an idea as to what might be wrong with the code. :) – Mateusz Hajdziony Nov 20 '15 at 4:44
  • 1
    Please, post the solution in the answers section. Answering your own question is really good and also will help other developers with same problem, which is the main purpose of this site. – cybmeta Nov 20 '15 at 5:39
1

As of @cybmeta word's, I answered my question here.

Thank you for all of your effort. In future, if someone comes to read this article, just remember, default arguments also affecting your results. So my final working code it.

 global $post, $paged, $current_user;
get_currentuserinfo();
if(empty($paged)) $paged = 1; 
    $args = array(
                'post_type' => 'tickets',
                'post_author' => $current_user->ID,
                'post_status' => 'any',  
                'paged'=> $paged
            );  
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :  setup_postdata($post);
            echo "test echo"; // testing purpose
            echo "<tr><td>".the_title()."</td><td>".the_excerpt()."</td><td>".$post->post_status."</td></tr>";
     endforeach; 
   wp_reset_postdata();
0

Hard to say without seeing the code. But ill give it a try. 1. ) If your not seeing any results of your published custom post type posts in the db, are you seeing them in WP Admin? 2. ) Double check your implementation of how your defining your $paged variable, should be get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; 3. ) You shouldn't need either of those global vars. 4. ) How have you setup the $current var used for author argument?

Some more details would make easier to help.

  • the following code retrieves results. $args = array( 'post_author' => $current_user->ID, 'paged'=> $paged ); . but if i add 'post_type' => 'tickets' its showing empty results. But i have posts in the tickets post type, i can see it on the back end – Kvvaradha Nov 20 '15 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.