Custom post type with sorting and filtering


/**
* Plugin Name: Custom Post Type Man
* Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
* Description: A brief description of the plugin.
* Version: The plugin's version number. Example: 1.0.0
* Author: Name of the plugin author
* Author URI: http://URI_Of_The_Plugin_Author
* Text Domain: customposttyoe
* Domain Path: Optional. Plugin's relative directory path to .mo files. Example: /locale/
* Network: Optional. Whether the plugin can only be activated network wide. Example: true
* License: A short license name. Example: GPL2
*/
add_action('init', 'create_portfolio_post_type');

function create_portfolio_post_type() {
$args = array(
'description' => 'Portfolio Post Type',
'show_ui' => true,
'menu_position' => 4,
'exclude_from_search' => true,
'labels' => array(
'name' => 'Portfolios',
'singular_name' => 'Portfolios',
'add_new' => 'Add New Portfolio',
'add_new_item' => 'Add New Portfolio',
'edit' => 'Edit Portfolios',
'edit_item' => 'Edit Portfolio',
'new-item' => 'New Portfolio',
'view' => 'View Portfolios',
'view_item' => 'View Portfolio',
'search_items' => 'Search Portfolios',
'not_found' => 'No Portfolios Found',
'not_found_in_trash' => 'No Portfolios Found in Trash',
'parent' => 'Parent Portfolio'
),
'public' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => true,
'supports' => array('title', 'editor', 'thumbnail', 'comments', 'tags')
);
register_post_type('portfolio', $args);
}

/* ====================================================
Register Custom Taxonomies
====================================================== */

add_action('init', 'register_portfolio_taxonomy');

function register_portfolio_taxonomy() {
register_taxonomy('portfolio_category', 'portfolio', array(
'labels' => array(
'name' => 'Portfolio Categories',
'singular_name' => 'Portfolio Categories',
'search_items' => 'Search Portfolio Categories',
'popular_items' => 'Popular Portfolio Categories',
'all_items' => 'All Portfolio Categories',
'parent_item' => 'Parent Portfolio Category',
'parent_item_colon' => 'Parent Portfolio Category:',
'edit_item' => 'Edit Portfolio Category',
'update_item' => 'Update Portfolio Category',
'add_new_item' => 'Add New Portfolio Category',
'new_item_name' => 'New Portfolio Category',
),
'hierarchical' => true,
'show_ui' => true,
'show_tagcloud' => true,
'rewrite' => false,
'public' => true
)
);
}

/* ====================================================
Register Custom Tags
====================================================== */
add_action('init', 'register_portfolio_tags');

function register_portfolio_tags() {
register_taxonomy('portfolio_tag', 'portfolio', array(
'hierarchical' => false,
'labels' => array(
'name' => _x('Portfolio Tags', 'taxonomy general name', 'customposttyoe'),
'singular_name' => _x('Portfolio Tag', 'taxonomy singular name', 'customposttyoe'),
'search_items' => __('Search Tags', 'customposttyoe'),
'popular_items' => __('Popular Tags', 'customposttyoe'),
'all_items' => __('All Tags', 'customposttyoe'),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __('Edit Portfolio Tag', 'customposttyoe'),
'update_item' => __('Update Portfolio Tag', 'customposttyoe'),
'add_new_item' => __('Add New Portfolio Tag', 'customposttyoe'),
'new_item_name' => __('New Portfolio Tag Name', 'customposttyoe'),
'separate_items_with_commas' => __('Separate Portfolio Tag with commas', 'customposttyoe'),
'add_or_remove_items' => __('Add or remove portfolio Tag', 'customposttyoe'),
'choose_from_most_used' => __('Choose from the most used portfolio Tag', 'customposttyoe')
),
'show_ui' => true,
'query_var' => true,
'rewrite' => false,
));
}

/* ====================================================
Add extra columns
====================================================== */

add_filter("manage_edit-portfolio_columns", "portfolio_edit_columns");

function portfolio_edit_columns($columns) {

$columns = array(
"cb" => "<input type='checkbox' />",
"title" => __("Portfolio"),
"photo" => __("Image"),
"portfolio_category" => __("Portfolio Category"),
"portfolio_tag" => __("Portfolio Tag"),
"portfolio_rating" => __("Portfolio Rating"),
"date" => __("Date")
);
return $columns;
}

add_action("manage_portfolio_posts_custom_column", "portfolio_custom_columns");

function portfolio_custom_columns($column) {
global $post;
switch ($column) {
case "photo":
if (has_post_thumbnail())
the_post_thumbnail(array(50, 50));
break;
case "portfolio_category":
echo get_the_term_list($post->ID, 'portfolio_category', '', ', ', '');
break;
case "portfolio_tag":
echo get_the_term_list($post->ID, 'portfolio_tag', '', ', ', '');
break;
case "portfolio_rating":
echo intval(get_post_meta($post->ID, 'portfolio_rating', true));
break;
}
}

/* ====================================================
Add filter on categories.
====================================================== */
if (isset($_GET['post_type'])) {
$post_type = $_GET['post_type'];
} else {
$post_type = '';
}

if ($post_type == 'portfolio') {
add_action('restrict_manage_posts', 'portfolio_filter_list');
add_filter('parse_query', 'perform_filtering');

add_action('restrict_manage_posts', 'portfolio_filter_tags');
}
/*
* Filter in select box category
*/
function portfolio_filter_list() {
global $typenow, $wp_query;
if ($typenow == 'portfolio') {
wp_dropdown_categories(array(
'show_option_all' => 'Show All Portfolio Category',
'taxonomy' => 'portfolio_category',
'name' => 'portfolio_category',
'orderby' => 'name',
'selected' => ( isset($wp_query->query['portfolio_category']) ? $wp_query->query['portfolio_category'] : '' ),
'hierarchical' => false,
'depth' => 3,
'show_count' => false,
'hide_empty' => true,
));
}
}
/*
* Filter in select box tags
*/
function portfolio_filter_tags() {
global $typenow, $wp_query;
if ($typenow == 'portfolio') {
wp_dropdown_categories(array(
'show_option_all' => 'Show All Portfolio Tags',
'taxonomy' => 'portfolio_tag',
'name' => 'portfolio_tag',
'orderby' => 'name',
'selected' => ( isset($wp_query->query['portfolio_tag']) ? $wp_query->query['portfolio_tag'] : '' ),
'hierarchical' => false,
'depth' => 3,
'show_count' => false,
'hide_empty' => true,
));
}
}
/*
* Filter in select box and fill data.
*/
function perform_filtering($query) {
$qv = &$query->query_vars;
if (isset($qv['portfolio_category'])) {
if (( $qv['portfolio_category'] ) && is_numeric($qv['portfolio_category'])) {
$term = get_term_by('id', $qv['portfolio_category'], 'portfolio_category');
$qv['portfolio_category'] = $term->slug;
}
}

if (isset($qv['portfolio_tag'])) {
if (( $qv['portfolio_tag'] ) && is_numeric($qv['portfolio_tag'])) {
$term = get_term_by('id', $qv['portfolio_tag'], 'portfolio_tag');
$qv['portfolio_tag'] = $term->slug;
}
}
}

/* ====================================================
Add Sortable columns
====================================================== */
add_filter('manage_edit-portfolio_sortable_columns', 'portfolio_sortable_columns');

function portfolio_sortable_columns($columns) {

$columns['portfolio_category'] = 'portfolio_category';
$columns['portfolio_tag'] = 'portfolio_tag';
$columns['portfolio_rating'] = 'portfolio_rating';
return $columns;
}
/* ====================================================
Add Sortable columns on Portfolio Rating
====================================================== */
add_action('pre_get_posts', 'portfolio_category_orderby');
function portfolio_category_orderby($query) {
if (!is_admin())
return;
$orderby = $query->get('orderby');

print_r($orderby);//die;
if ('portfolio_category' == $orderby) {
$query->set('taxonomy', 'portfolio_category');
}
}
/* ====================================================
Add Sortable columns on Portfolio Rating
====================================================== */
add_action('pre_get_posts', 'portfolio_rating_orderby');
function portfolio_rating_orderby($query) {
if (!is_admin())
return;
$orderby = $query->get('orderby');
if ('portfolio_rating' == $orderby) {
$query->set('meta_key', 'portfolio_rating');
$query->set('orderby', 'meta_value_num');
}
}

/* ====================================================
* Function creates post duplicate as a draft and redirects then to the edit post screen
====================================================== */
add_filter('post_row_actions', 'rd_duplicate_post_link', 10, 2);

function rd_duplicate_post_as_draft() {
global $wpdb;
if (!( isset($_GET['post']) || isset($_POST['post']) || ( isset($_REQUEST['action']) && 'rd_duplicate_post_as_draft' == $_REQUEST['action'] ) )) {
wp_die('No post to duplicate has been supplied!');
}

/*
* get the original post id
*/
$post_id = (isset($_GET['post']) ? $_GET['post'] : $_POST['post']);
/*
* and all the original post data then
*/
$post = get_post($post_id);

/*
* if you don't want current user to be the new post author,
* then change next couple of lines to this: $new_post_author = $post->post_author;
*/
$current_user = wp_get_current_user();
$new_post_author = $current_user->ID;

/*
* if post data exists, create the post duplicate
*/
if (isset($post) && $post != null) {

/*
* new post data array
*/
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $new_post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => 'draft',
'post_title' => $post->post_title,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
);

/*
* insert the post by wp_insert_post() function
*/
$new_post_id = wp_insert_post($args);

/*
* get all current post terms ad set them to the new post draft
*/
$taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
foreach ($taxonomies as $taxonomy) {
$post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
}

/*
* duplicate all post meta
*/
$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
if (count($post_meta_infos) != 0) {
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
foreach ($post_meta_infos as $meta_info) {
$meta_key = $meta_info->meta_key;
$meta_value = addslashes($meta_info->meta_value);
$sql_query_sel[] = "SELECT $new_post_id, '$meta_key', '$meta_value'";
}
$sql_query.= implode(" UNION ALL ", $sql_query_sel);
$wpdb->query($sql_query);
}
/*
* finally, redirect to the edit post screen for the new draft
*/
wp_redirect(admin_url('post.php?action=edit&post=' . $new_post_id));
exit;
} else {
wp_die('Post creation failed, could not find original post: ' . $post_id);
}
}

add_action('admin_action_rd_duplicate_post_as_draft', 'rd_duplicate_post_as_draft');

/* ====================================================
* Add the duplicate link to action list for post_row_actions
* ====================================================== */

function rd_duplicate_post_link($actions, $post) {
if ($post->post_type == "portfolio"):
$actions['duplicate'] = '<a href="admin.php?action=rd_duplicate_post_as_draft&amp;post=' . $post->ID . '" title="Duplicate this item" rel="permalink">Duplicate</a>';
endif;
return $actions;
}

/* ====================================================
* Add the custom meta box
* ====================================================== */

add_action('admin_init', 'my_admin');

function my_admin() {
add_meta_box('portfolio_review_meta_box', 'Portfolio Review Details', 'display_portfolio_review_meta_box', 'portfolio', 'normal', 'high'
);
}

function display_portfolio_review_meta_box($portfolio_review) {
// Retrieve current name of the Director and Portfolio Rating based on review ID
$portfolio_director = esc_html(get_post_meta($portfolio_review->ID, 'portfolio_director', true));
$portfolio_rating = intval(get_post_meta($portfolio_review->ID, 'portfolio_rating', true));
?>
<table>
<tr>
<td style="width: 100%">Portfolio Director</td>
<td><input type="text" size="80" name="portfolio_review_director_name" value="<?php echo $portfolio_director; ?>" /></td>
</tr>
<tr>
<td style="width: 150px">Portfolio Rating</td>
<td>
<select style="width: 100px" name="portfolio_review_rating">
<?php
// Generate all items of drop-down list
for ($rating = 5; $rating >= 1; $rating --) {
?>
<option value="<?php echo $rating; ?>" <?php echo selected($rating, $portfolio_rating); ?>>
<?php echo $rating; ?> stars <?php } ?>
</select>
</td>
</tr>
</table>
<?php
}

add_action('save_post', 'add_portfolio_review_fields', 10, 2);

function add_portfolio_review_fields($portfolio_review_id, $portfolio_review) {
// Check post type for portfolio reviews
if ($portfolio_review->post_type == 'portfolio') {
// Store data in post meta table if present in post data
if (isset($_POST['portfolio_review_director_name']) && $_POST['portfolio_review_director_name'] != '') {
update_post_meta($portfolio_review_id, 'portfolio_director', $_POST['portfolio_review_director_name']);
}
if (isset($_POST['portfolio_review_rating']) && $_POST['portfolio_review_rating'] != '') {
update_post_meta($portfolio_review_id, 'portfolio_rating', $_POST['portfolio_review_rating']);
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s