Get Categories Author has Posted in for Custom Post Types

I needed to list all the categories of a custom post type that an author had posted in.

I found code to do that with regular posts in the wordpress.org forums and I modified it to use with custom post types.

Be sure to replace customposttype with the name of your custom post type.

Place this into your theme file where you want the list to appear.

<?php
$author = get_the_author_meta('ID');

$categories = $wpdb->get_results("
SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug
FROM $wpdb->posts as posts
LEFT JOIN $wpdb->term_relationships as relationships ON posts.ID = relationships.object_ID
LEFT JOIN $wpdb->term_taxonomy as tax ON relationships.term_taxonomy_id = tax.term_taxonomy_id
LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
WHERE 1=1 AND (
	posts.post_status = 'publish' AND
	posts.post_author = '$author' AND
	tax.taxonomy = 'customposttype' )
ORDER BY terms.name ASC
");
?>
<ul>
<?php foreach( $categories as $category ) : ?>
	<li>
		<a href="<?php echo get_term_link( $category->name, 'customposttype' ); ?>" title="<?php echo $category->name ?>"><?php echo $category->name ?></a>
	</li>
<?php endforeach; ?>
</ul>


New To-Do List Plugin Version

Updated the To-Do List Plugin with an updated German translation by Ascobol.


Limit Number of Posts Per User in WordPress

This code will limit the number of posts a user can make.

At first, I was trying to limit each user to one post and I had that working. Then I realized that I needed to have different numbers of posts for different users, so I created an option in a user’s profile that only people with the right capability (manage_options) can edit. I’ll be changing this in the future for that number to be automatically changed depending on a user’s subscription type, but I needed this for testing purposes.

The option in the profile shows up under the contact info section, which I know is an odd place, but it was the easiest place to put it.

Put the following into your functions.php file:

// limit number of posts per user
// provided by cleverness.org
if ( !current_user_can('manage_options') ) {
	$default = 1; // default number of posts
	$count_posts = 0;
	global $wpdb;
	$poststable = $wpdb->prefix.'posts';
	$postlimit = get_user_meta($current_user->id, 'postlimit', true);
	if ( $postlimit == '' ) $postlimit = $default;
	$query = "SELECT COUNT(*) FROM $poststable WHERE post_author = '$current_user->id' AND (post_status = 'pending' OR post_status = 'draft' OR post_status = 'publish' ) ";
	$count_posts = $wpdb->get_var($query);
	if ($count_posts >= $postlimit) {
		if ( $_SERVER['REQUEST_URI'] == '/wp-admin/post-new.php' )
			Header("Location: index.php");//redirects to dashboard
		if ( is_admin() ){
			$stylesheet =  get_stylesheet_directory_uri() . '/css/limitposts.css';
   			wp_register_style('limitpost_admin_css', $stylesheet, false, '1', 'screen');
   			wp_enqueue_style('limitpost_admin_css');
			}
	}
}

// add post limit option to profile
add_filter('user_contactmethods','hide_profile_fields',10,1);

function hide_profile_fields( $contactmethods ) {
	if( current_user_can( 'manage_options' ) )
		$contactmethods['postlimit'] = 'Post Limit';
  	return $contactmethods;
}

Create a file called limitposts.css, put the following into it, and save it in a directory called css in your theme folder (you can change where you save it, but be sure to change the location in functions.php).

#menu-posts .wp-first-item + li {
	display: none;
}

If you want to use this for custom post types, change:

if ( $_SERVER['REQUEST_URI'] == '/wp-admin/post-new.php' )

to:

if ( $_SERVER['REQUEST_URI'] == '/wp-admin/post-new.php?post_type=customposttype' )

and change the id name in your CSS:

#menu-posts-customposttype .wp-first-item + li {
	display: none;
}

Be sure to replace customposttype with the name of your custom post type.


To-Do List Plugin version 2.1.3

Version 2.1.3 of the To-Do List Plugin is out.

This is just a small bug fix that fixes the backslash appearing before apostrophes on some servers.


Add User Types to WordPress and BuddyPress

I’ve been working on some more complicated sites than usual at work. One is a social networking site that uses BuddyPress. It’s my first BuddyPress site, so it’s been a learning experience. Additionaly, it was my first WordPress Multi-Site.

One thing I needed to do was allow people to choose a user type at registration. I also needed the user to be assigned a custom Role based on their user type.

I used the Capability Manager Plugin to set the custom roles.

The code is based off How to add custom $usermeta to registration and How to change a users role on the wordpress registration form.

Change the User Type A, user-type-a, etc. to whatever you want your user types to be.

Put this in you BuddyPress theme /registration/register.php file:

<label>Account Type:</label>
<select>
	<option value="user-type-a">User Type A</option>
	<option value="user-type-b">User Type B</option>
	<option value="user-type-c">User Type C</option>
</select>

Put the following code in your theme’s functions.php:

<?php
/* Add sign-up field to BuddyPress sign-up array*/
function bp_custom_user_signup_field( $usermeta ) {
	$usermeta['signup_type'] = $_POST['signup_type'];

	return $usermeta;
}
add_filter( 'bp_signup_usermeta', 'bp_custom_user_signup_field' );

/* Add field_name from sign-up to usermeta on activation */
function bp_user_activate_field( $signup ) {

	update_usermeta( $signup['user_id'], 'signup_type', $signup['meta']['signup_type'] );

	return $signup;
}
add_filter( 'bp_core_activate_account', 'bp_user_activate_field' );

function synchro_wp_usermeta($user_id, $password, $meta) {
	global $bp, $wpdb;

	$type = $meta[signup_type];

	update_usermeta( $user_id, 'signup_type', $type );

}
add_action( 'wpmu_activate_user', 'synchro_wp_usermeta', 10, 3);

// change user role on registration

function register_role($user_id, $password, $meta) {

	global $bp, $wpdb;

   	$userdata = array();
   	$userdata['ID'] = $user_id;
   	$userdata['role'] = $meta[signup_type];

   	//only allow if user role is my_role

   	if ($userdata['role'] == 'user-type-a' || $userdata['role'] == 'user-type-b' || $userdata['role'] == 'user-type-c' ){
      	wp_update_user($userdata);
   	}
}

add_action('wpmu_activate_user', 'register_role', 10, 3);
?>

You can use get_user_meta() in your theme files to show different things to different user types.

<?php
global $bp;
if ( get_user_meta($bp->displayed_user->id, 'signup_type') == 'user-type-a' ) : ?>
	Put your HTML code here
<?php endif; ?>
?>


To-Do List Plugin version 2.1.2

Download To-Do List Plugin – version 2.1.4 posted 08-21-2010

I had a failed experiment with trying to hide certain options in the To-Do List Plugin when other options weren’t enabled. This appears to cause an issue with storing the options in an array in one field in the options table.

I’ve taken it out and options should be saving properly.


To-Do List Plugin version 2.1.1

Download To-Do List Plugin – version 2.1.4 posted 08-21-2010

The To-Do List Plugin has been updated.

There was a bug in the shortcode that caused it to not appear in the proper place in the post or page. This was due to me using echo statements instead of return.

I also added an option to customize the subject and text of the emails that are sent to users that are assigned tasks if that option is enabled. These options won’t appear unless you Save Changes after setting Email Assigned Task to User to Yes.


To-Do List Plugin version 2.1

Download To-Do List Plugin – version 2.1.4 posted 08-21-2010

The latest version of the To-Do List Plugin for WordPress has been released.

The menu item for the to-do list has been moved to a top-level menu. The setting page is now located under that menu.

New in this version are category support and the ability to set the sort order.

Categories are site-wide at the moment so when using Individual View, all categories will be visible to all users.


Smashing WordPress Review

Smashing WordPress Review

Smashing WordPress: Beyond the Blog (Smashing Magazine Book Series) by Thord Daniel Hedengren

This is not a book for beginners. You should know HTML and CSS (and some basic PHP) and be comfortable with editing files.

I’ve used the loop before for custom queries, but I found the way he explained it easier to comprehend than other explanations I’ve read.

I felt like some of the code displayed in the book was unnecessary, especially most of the CSS. There were a few things I would have liked to see the code for that weren’t shown, mainly in the second half of the book.

I would have loved to see the Uncommon WordPress Usage chapter expanded, with more examples and code.

It’s too bad that the book came out before 3.0 and custom post types and menus. It just came out in February, so it is pretty up to date but WordPress evolves so fast.

There were some complaints on amazon.com about the layout and design of the book but I have an issues reading it.

I did learn a few new things from the book, but mostly I found it inspiring.

I didn’t see any website listed in the book to download the code, but I did find it via Google. You can download the Smashing WordPress code from the author’s website. The only issue is that problems noted in the comments there has not been fixed yet (the question marks are replaced with a different character). But search and replace is still faster than retyping it all.

A lot of the code is taken from his theme, Notes Blog Core. I like that he uses his theme as an example, rather than making up a fake theme. It makes everything more real.

I would highly recommend this book.


To-Do List Plugin – Possible New Feature

As I’ve been getting a new site of mine ready for launch, I realized that I have a need for both a public and a private to-do list.

I often use my to-do lists as places for possible post topics, things I need to do to improve the site, and so on. This new site is going to have reviews and I want to list upcoming reviews. And to be able to see the upcoming list in the admin so I know what I need to do and check them off when I finish (or I would just use a text widget).

So I’ve been thinking of how exactly I should change my plugin. Do I just make a private checkbox? Do I set it up to allow categories? With category options where you can set it as public or private?

Right now I’m liking the category idea best. Does anyone have any input?