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 wpdreamer.com.

Improved Custom Queries

By default WordPress wants you to use the WP_Query functionality and goof around with their global queries. Unless you are literally just displaying a listing of a specific post types, and that is all you are doing on that page, this is cumbersome and REALLY ugly.

I have created some functionality to query posts using some cleaner functions. Check it out.

NOTE: I use static variables here that require some functions from a previous post.

Now all you have to do to get your posts just call the function.

Now, the most important reason I use this function is not just to get the queries, but ALSO to setup custom pagination. Here is the function needed to display your custom pagination.

Now all you have to do to display your custom pagination is call the function.

When put together this is basically the code you will be using.

There we go! A cleaner way to handle custom post types on a page.

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):
Screen-Shot-2014-10-17-at-9.25.39-AM

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.