Archive | April 2012

WordPress Custom Post Type Archive

I have problem showing custom post type archive page. I want to share the code to create a custom widget which you can input the post type name and it will show you the archive (year and month) for that custom post type. You can edit it as needed.

<?php
class custom_archive extends WP_Widget {
function custom_archive() {
parent::WP_Widget(false, 'Custom Archive');
}
function form($instance) {
// outputs the options form on admin
$post_type = $instance['post_type'];
$title = $instance['title'];
?>
<p>Title:<br /> <input name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
<p>Post Type:<br /> <input name="<?php echo $this->get_field_name( 'post_type' ); ?>" type="text" value="<?php echo esc_attr( $post_type ); ?>" /></p>
<?php
}
function update($new_instance, $old_instance) {
// processes widget options to be saved
$instance = $old_instance;
$instance['post_type'] = strip_tags( $new_instance['post_type'] );
$instance['title'] = strip_tags( $new_instance['title'] );
return $instance;
}
function widget($args, $instance) {
// outputs the content of the widget
extract( $args, EXTR_SKIP );
$post_type = $instance['post_type'];
$title = $instance['title'];
?>
<?php echo $before_widget; ?>
<?php echo $before_title . $title . $after_title; ?>
<ul>
<?php
global $wpdb;
/**/
$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = '".$post_type."' ORDER BY post_date DESC");
foreach($years as $year) :
$years_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = '".$post_type."' AND YEAR(post_date) = ".$year.";" ) );
?>
<li class="year"><a href="<?php echo get_year_link($year)."?post_type=".$post_type; ?>"><?php echo $year; ?></a> (<?php echo $years_count; ?> Posts)
<ul>
<? $months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = '".$post_type."' AND YEAR(post_date) = '".$year."' ORDER BY post_date DESC");
foreach($months as $month) :
$months_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = '".$post_type."' AND YEAR(post_date) = ".$year." and MONTH(post_date) = ".$month.";" ) );
?>
<li class="month">>
<a href="<?php echo get_month_link($year, $month)."?post_type=".$post_type; ?>"><?php echo date( 'F', mktime(0, 0, 0, $month) );?></a> (<?php echo $months_count; ?> Posts)
</li>
<?php endforeach;?>
</ul>
</li>
<?php endforeach; ?>
</ul>
<?php echo $after_widget; ?>
<?php
}
}
register_widget('custom_archive');
?>

“You do not have sufficient permissions to access this page” after database prefix change

After several times moving WordPress websites from one host to another, I get this painfully permission denied for superadmin user “You do not have sufficient permissions to access this page”. The frontend page was all fine. But I can’t access the admin page. Then I found these people who have the same problem and found the solution (http://wordpress.org/support/topic/sudden-you-do-not-have-sufficient-permissions-to-access-this-page-error).

The main cause is the database prefix changes. It cause the wordpress doesn’t recognize the admininstrator privilege. if you look into the wp_options table, the option_name from roles is wp_user_roles. The wp_ should match the table prefix. So if you change the wp_options table to en_options, then you must change the options_name to en_user_roles. That’s how the wordpress recognize the roles.

Besides that, user to role mapping also use the table prefix. You can see it in wp_usermeta tables. There is a meta_key called wp_capabilities. The wp_ is the table prefix of the wordpress tables. So if you change the table prefix e.q. wp_usermeta to en_usermeta, then you should change the wp_capabilities to en_capabilities.

Thats how you fix the permission problem to wp_admin page.

Have a good day

Get Taxonomy List without links

Use <?php wp_get_object_terms( $object_ids, $taxonomies, $args ) ?>

http://codex.wordpress.org/Function_Reference/wp_get_object_terms