Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I try to create an admin page where to display all posts from all custom post type. It is something like in edit.php, but instead of displaying single post type,I would like to display all posts in one page from multiple custom post type.

I follow the tutorial at here to create the table. Everything is fine, but there is somethings that I can't solve. Actually the problem mostly in php rather than WP itself.

Here is where I trying to do : query the posts from the desired custom post types,and from the object return store it in array for further use. Here is my codes :

function getallposts() {
    global $current_user; // i need to show the posts depends on the user id.
    get_currentuserinfo();


     if(!current_user_can('administrator') && current_user_can('edit_posts') ) {
         $userid = $current_user->ID;
         } elseif(current_user_can('administrator')) {$userid='1';} // administrator can view all the posts, but the users only view their own posts

   $args = array(
   'numberposts'     => -1,
   'offset'          => 0,
   'orderby'         => 'post_date',
   'order'           => 'DESC',
   'post_type'       => array('cpt1','cpt2'),//post types that i need to query
   'author'          => $userid,//this is refer by the user login info above
   'post_status'     => 'publish' );
    $myposts = get_posts( $args );//here using get_posts to query the post.

    $myitems =array();//declare the the array.
    foreach ($myposts as $post) {
    $metavalue = get_post_meta($post->ID, 'rating', true);//get the meta value for the post

    $myitems =array (
    'post_title' => $post->post_title,
    'ID'=>$post->ID, 
    'post_author'=> $post->post_author,
    'post_date' => $post->post_date,
    'rating' =>$metavalue
    ); // i want to store it in an array for further use.don't know if I am doing right.

}return $myitems;//here is i don't get the idea to return the array from $myitems above}

If the code above is too complicated, in a nutshell here is what I trying to do:

function getallposts { //get the posts query in a function
          $args = array (); 
          $allpost = get_posts($args);//here is to get the posts by argument form $args
        foreach ($allpost as $post){
         $post_title = $post->post_title...
          .....  //define some of the  object that needed for further use.
         $value = array($post_title.....);//the defined object is store in an array for further use.
        }

        return $value //return the value//here is I don't know how to return the value.

}

My intention is to sort the $value in this way:

$example_data = array(
  array('ID' => 1,'booktitle' => 'Quarter Share', 'author' => 'Nathan Lowell',
    'isbn' => '978-0982514542'),
  array('ID' => 2, 'booktitle' => '7th Son: Descent','author' => 'J. C. Hutchins',
    'isbn' => '0312384378'),
  array('ID' => 3, 'booktitle' => 'Shadowmagic', 'author' => 'John Lenahan',
    'isbn' => '978-1905548927'),
  array('ID' => 4, 'booktitle' => 'The Crown Conspiracy', 'author' => 'Michael J. Sullivan',
    'isbn' => '978-0979621130'),
  array('ID' => 5, 'booktitle'     => 'Max Quick: The Pocket and the Pendant', 'author'    => 'Mark Jeffrey',
    'isbn' => '978-0061988929'),
  array('ID' => 6, 'booktitle' => 'Jack Wakes Up: A Novel', 'author' => 'Seth Harwood',
    'isbn' => '978-0307454355')
);

Thanks

share|improve this question
    
okay,turn out I forgot add [] to $myitems, it should be $myitems[]; –  dev-jim Aug 10 '12 at 19:55
add comment

1 Answer

You can use a $wpdb->get_results for this. I am not an mySQL expert so I will give you the pseudo code instead

global $wpdb;
$data = $wpdb->get_results(
    SELECT posts.ID ID, posts.post_title title, posts.post_author author, postmeta.isbn isbn
    FROM $wpdb->posts posts
    JOIN $wpdb->postmeta postmeta
        ON posts.ID = postmeta.post_id
    WHERE posts.post_type = 'post_type_1' OR 'post_type_2' OR 'post_type_3'
    GROUP BY posts.ID
    ORDER BY ".$orderby." ".$order." LIMIT ".$offset.",".$per_page,'ARRAY_A' ); 
)
share|improve this answer
    
I managed to get it right now. yes, u can use $wpdb->get_results, but get_posts also can do the job. In my code above is working except I forgot to add [] in the $myitems in foreach loop. It should be $myitems[]. Anyway, thanks... –  dev-jim Aug 11 '12 at 10:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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