Limit User Role to Certain Posts

There are a variety of plugins out there for managing user role access. User Role Editor (URE) and Advanced Access Manager (AAM) are both good plugins I can recommend. URE is great for…editing roles. The problem with URE is that it doesn’t allow more narrow scope restrictions…like a single post. AAM is great at limiting individual posts and hiding admin menus. AAM however requires a LOT of setup if you have a lot of pages / a lot of roles and starts to become unpractical for highly restrictive page editing.

All that said, I ended up turning to good ol’ taxonomies.

First, setup your taxonomy for your posts. In this case I am targeting a “Departments” custom post type.

See jp_register_cpt and jb_register_taxonomy for info on these functions.

Second, setup your roles with URE. I’ll leave this to you, but I usually start with copying either the “Contributor” or the “Subscriber” default roles and going from there. Mine looks like this:

Third, Add a new admin taxonomy in the new taxonomy you just created. Make sure to give it the same slug as the user role ID, this is just for ease of use later and for reference. Example:

Forth, add this function to your PHP file.

All done! Now this user role should not be able to view or edit any posts that do not have this category.

Sort Posts in Admin by Meta Value

Sometimes we need to display some meta information of a post in the admin listing. Usually it is handy to also make this information sortable so it is more useful to the client.

Here is some example code of how that is done.

You will need to update the ‘post-type’ to the post type you want to edit and the ‘my_meta_key’ to the meta key you want to sort by.

Big thanks to Rachel Carden over at

Exclude Post Types From Search

Sometimes I need / want to hide some post types from the built in WordPress search functionality. With a Custom Post Type this is really easy since you can just set the ‘exclude_from_search’ option when you register the post type.

However, some plugins register their own post types and sometimes we need to avoid searching these. Below is an example of excluding the Tribe Event Venues from search.

Thanks to oneTarek for the code.

Admin CPT Meta Value Filter

Today I needed to filter some Custom Post Types (CPTs) by their meta value for a client.

First we need to setup the filter select element.

Next we just need to filter these post types if we are on the right admin page

And done! Now you can filter your CPTs by some specified meta value.


Update All ACF Post Fields

I use ACF (Advanced Custom Fields) in every site I build. Sometimes I need to transfer some posts from one site to another for various reasons. When you use the standard WordPress Export / Import of posts it does bring over the correct ACF values but the references to those values are not in place and so they are not available on the front end of the site (or anywhere on the site except in the post edit page itself). Usually there are WAY too many posts to go through and update them all one at a time. Here is a small function which will go through and update all of your ACF post fields at once so you do not need to manually update the posts.

You can call this function however you want. Usually it is done by using an action or something.

This function can run a long time so if you use this method make sure to comment out or remove the function / action after it runs.

Custom Post Creation Form

In my last post I put up some code for displaying a CPT as a “Pinterest type” wall using Wookmark. In this post I am going to put some code for creating a form for uploading a new post from an anonymous user.

Once finished it should look something like this (client name censored):

NOTE: Will look slightly different in IE8 but is still fully functional.

Because we are actually doing the validation ourselves for this instead of relying on a plugin like gravity forms I am just going to paste the entire page code so you can see it from top to bottom. I think it would be a little confusing broken up.

While I do include both javascript and PHP validation here please remember that there are a lot of smart assholes out there who may want to wreck your shit for no reason. Make sure you test the validation so it meets your standards.

Also, there are a couple javascript functions here and they are doing different things. If you read the function names and the comments it should be obvious what each part is doing. I included the needed code for <noscript> in case the user has disabled javascript since otherwise the form would not be functional.

Let me also draw your attention to the dropdown menu and the “Share an Image” section. The dropdown is a custom dropdown menu controlled with javascript. The “Share an Image” section is actually pretty neat and I am going to make a post explaining that by itself.

Screen Shot 2014-10-17 at 9.37.44 AM

There is a whole lot going on here and since this is not a tutorial site I am not going to walk you through it but hopefully it all makes sense.

We do have one more step to do of course. We need some Style. Here is some LESS

Notice I am setting some fonts to Futura. This is a font I have in my assets folder at the web root. You will probably have to figure out what to do with the fonts. I would check out Google Fonts which is what I normally use on my sites.

Hopefully this post can help you setup your own form for allowing users to upload their own content.