Uploading images in custom post form

Let’s presume your FILE variable is called ‘files’ and can contain multiple inputs (i.e. name=”files[]”) 1) First upload the files $uploads = wp_upload_dir(); $upload_file_url = array(); foreach ($_FILES[‘files’][‘name’] as $key => $value_data) { $errors_founds=””; if ($_FILES[‘files’][‘error’][$key] != UPLOAD_ERR_OK) $errors_founds .= ‘Error uploading the file!<br />’; if (!array_key_exists($_FILES[‘files’][‘type’][$key], $mime)) $errors_founds .= ‘Invalid file type!<br />’; if … Read more

Check if a custom post type has already been created

To check for the existence of a post type, use post_type_exists. For instance like this: if ( ! post_type_exists( ‘yourpt’ ) { $args = array( // your new post type’s arguments ); register_post_type( ‘yourpt’, $args ) } As for the prefix, that’s a matter of personal taste and how common you think the post type … Read more

How can I list posts by author?

This is a simpler solution, if you want a total control over the code and don’t want to use a query. $authors = get_users(‘role=author’); if(isset($authors) && !empty($authors)) { echo “<ul>”; foreach($authors as $author) { $posts = get_posts(array(‘author’=>$author->ID)); //if this author has posts, then include his name in the list otherwise don’t if(isset($posts) && !empty($posts)) { … Read more

Modify posts listing at back end

You can do this easily using the edit_posts_per_page hook or edit_{$post_type}_per_page hook for pages and custom post types You can change based on the user role: function my_edit_post_per_page_role_based( $per_page, $post_type ) { //admin sees 25 posts per page if (current_user_can(‘administrator’)){return 25;} //editor sees 20 posts per page elseif(current_user_can(‘editor’)){return 15;} //author sees 10 posts per page … Read more

Get attachments only for this post

Try this using the get_children function: global $post; $args = array( ‘numberposts’ => -1, ‘order’=> ‘ASC’, ‘post_mime_type’ => ‘image’, ‘post_parent’ => $post->ID, ‘post_status’ => ‘any’, ‘post_type’ => ‘attachment’ ); $attachments = get_children($args); Reference: http://codex.wordpress.org/Function_Reference/get_children