loading

Post association with taxonomies

[Fun with code is a multi-part series. Read the rest.]

[You're reading part 3 (of 3). Read part 1. Read part 2.]

It’s been a little while since the last entry to Fun with code, mostly because I’ve been having quite a bit of fun with code. Alas, this tutorial has a third and final part which will wrap everything up.

Dangerous Territory

Now, here is where I must warn you that the tutorial falls into the area of highly advanced. This function took many hours and many different versions to perfect. The result is a series of looping functions which build and compare arrays in order to properly fill the custom taxonomy with the necessary terms. It also uses the arrays to check whether or not a term should be updated or deleted when its corresponding post is deleted.

As I said before, this one is highly complex, and because of that level of complexity, it can be easy to get lost. This time, I’ll simplify my explanations and provide them inline, directly inside of the code snippet. Look for the light-blue text lines that begin with slashes. If you need any information or specifics on any of the WordPress functions mentioned below, make sure you visit the WordPress codex.

<?php
function custom_register_terms() {
// First, set up the arguments for the WordPress function, get_posts().
	$args = array('post_type' => 'products', 'numberposts', -1);
// Run get_posts() and attach it's output array to the variable, $products.
	$products = get_posts($args);
// Start an empty array which we will fill later.
	$lockdown_products = array();
// Begin running through our array of product posts with a foreach loop.

/* A foreach loop is a special method which allows us to take each part of 
an array and run the same group of functions over each one individually. */

// Treat each part of the $products array as an individual piece called $product.
	foreach ( $products as $product ) {
// Save each product's title to a simpler array for comparison purposes later.
		$lockdown_products[$product->post_name] = $product->post_title;
// Use WordPress' get_term_by() function to see if we've already created the association.
		if ( get_term_by('slug', $product->post_name, 'association') == false ) {
// If not, create it using wp_insert_term().
			$insert = array('slug' => $product->post_name);
			wp_insert_term( $product->post_title, 'association', $insert );
		}
	}

/* Now that we're done creating any new terms, let's clean up the existing ones. */

// Get all of the existing association terms and set them to an array called $assocs.
	$assocs = get_terms('association', array('get' => 'all') );
// Run through the new array to see if we need to delete or update anything.
	foreach ( $assocs as $assoc ) {
		if ( $lockdown_products[$assoc->slug] == '') {
// If the current term's post no longer exists, delete it using wp_delete_term().
			wp_delete_term($assoc->term_id, 'association');
		} elseif ( $assoc->name !== $lockdown_products[$assoc->slug] ) {
// Otherwise, update it using wp_update_term(), just in case something has changed.
			$update = array(
				'name' => $lockdown_products[$assoc->slug],
				'slug' => $assoc->slug
			);
			wp_update_term($assoc->term_id, 'association', $update);
		}
	}
}
// Hook the function into WordPress, run it every time something is done to a post.
add_action( 'save_post', 'custom_register_terms', 1);
?>

What we’ve finally accomplished here is a nice, easy method to add a simple interface item which allows us to associate one post to another through WordPress. The possibilities here are endless, and in the case of the new Great Plains Motion Picture Company website, can be quite useful for organizing content and displaying it to the end-user.

1 Comment

13 Apr 2012

Reply

I really liked your way of “fun with code”, however you have a different way of coding.
But I wanted to ask you for help if you can; I am working in a website with a front-end posting using custom post type with attaching image and custom taxonomies, every thing work good except the front custom taxonomy.
I would appreciate if you can give advice Or an example of injecting the custom taxonomy to the custom post type from the front-end.
yours,

22 Sep 2019

‘Enter’ to submit

SVG is a thing now; you should use it.

With all this talk about resolution independence and responsive design, how many times have you built something for the internet and thought, hey, it sure would be nice if I could have a vector graphic here instead of a series of pre-saved bulky images switching in and out some way or another? For example: every single logo ever, social media buttons, line art of any type. If your concern is responsiveness and you’re using images, then you’re in for trouble.…

The multiply effect is a lie

This year’s fabulous Big Omaha website is showcasing a little visual trick. Early on, when Nathan and I were discussing the intended visuals on the site, I failed to notice he was using the multiply effect in Photoshop to achieve the appearance of the red overlays – and he went forward working that into the design. To my dismay, when I went to add the red layers by simply overlaying a slightly transparent layer of red over the top of…

Fun with code at Meet The Pros

Last week I spoke at Meet The Pros and, not surprisingly, my presentation was titled Fun with code. When I had volunteered to speak way back when, I envisioned myself talking about a bunch of code stuff and making it really interesting and fun. If you’re a regular reader of the Fun with code series here, you probably don’t remember any of it being terribly interesting or fun. It may be really helpful and useful, but it’s certainly too specialized…

Detecting a retina display

A few weeks ago, we announced that oxidedesign.com was fully retina optimized. For those of you who do not know, a retina display is a monitor or device that has more pixels per inch of space than a normal display. This basically means it is harder to tell where the edges of pixels are, allowing for an extremely crisp and clean appearance. This works great on the web for things like text, but a problem arises when displaying images. Since…

Access Restriction for Live Staging Sites

We have a lot of active, ongoing projects here at Oxide, especially when it comes to the web. In order to build and make changes to sites which can still be viewable for internal or client review, we run live staging versions of them on subdomains of oxidedesign.com. Don’t bother digging around looking for a secret gem though, because you can’t get in. At least, not anymore.

Extend post types with custom taxonomies

For part two of this series, I want to show how I extended the default functionality of WordPress custom post types and taxonomies in order to build a user-friendly way to associate posts from one type to another. To recap, in part one, we set up a couple of custom post types. To start this tutorial we’ll be using a very similar function to set up the taxonomy which we will leverage for post-to-post association.

Using custom post types for WordPress

Over the past few years, I’ve written quite a bit of code and I’ve had a lot of fun doing it. I’ve got tons of useful snippets in my back pocket and I’m just itching to share them with the world. I’m going to take this opportunity to start a new series here on the Oxide blog, Fun with code. To get things started, let’s outline a problem and uncover a situation which creative code can solve.