Magento WordPress Integration Magento WordPress Integration

Advanced Custom Coding

This article explains how to work with the models, resources and collections offered by Magento WordPress Integration. This article will be constantly updated as new code is added. If there's some code you want to see here, please get in touch.

Is there anything we have missed? Please get in touch and let us know!

Post Models (including pages and custom post types)

In WordPress, posts are used to represent the standard blog posts but are also used to represent pages. At it's basic form, a page is just a post with it's 'post_type' field set to 'page'. You can also create custom post types, which are just posts with their 'post_type' field set to what ever you decide to call your new custom post type.

In Magento WordPress Integration, posts, pages and custom post types are all represented by the Fishpig_Wordpress_Model_Post class and all have their post_type field set accordingly.

To make use of custom post types, you will need to install the Post Types & Taxonomies add-on extension for Magento.

// Load a post model with the ID 1.
// As no post_type was set, this defaults to 'post'
$post = Mage::getModel('wordpress/post')->load(1);

// Load a post with type 'page' with the ID 1.
$post = Mage::getModel('wordpress/page')->setPostType('page')->load(1);

// Load a post with type 'article' with the ID 1.
// This requires the Post Types and Taxonomies add-on
$post = Mage::getModel('wordpress/page')->setPostType('article')->load(1);

// Load a post of any type with the ID of 1.
$post = Mage::getModel('wordpress/page')->setPostType('*')->load(1);

/**
 * Get a post's featured image and display different sizes
**/
if ($image = $post->getFeaturedImage()) {
	// Get the URL of the thumbnail image
	echo $image->getThumbnailImage();
	
	// Get the URL of the medium sized image
	echo $image->getMediumImage();
	
	// Get the URL of the large image
	echo $image->getLargeImage();
	
	// Get the URL of the full size image
	// This will be the original uploaded image size
	echo $image->getFullSizeImage();
	
	// Get the post thumbnail image URL
	echo $image->getPostThumbnailImage();
	
	// Work through these images and get the first available image
	echo $image->getAvailableImage();
}

/**
 * Get data from a post object
**/
// Get the post's permalink
echo $post->getPermalink();

// Get the Author/user model for the post
$author = $post->getAuthor();

// Determine whether the post has been published
echo (int)$post->isPublished();

// Get the children posts associated with $post
// This is used by pages (post models with post_type=page)
if ($post->hasChildrenPosts()) {
	$childPosts = $post->getChildrenPosts();
}

/**
 * Get a meta (custom field) value for a post 
 * If you have Advanced Custom Fields installed, the same code works
 * and will check ACF for the custom field value first
**/
echo $post->getMetaValue('my_custom_field');

/**
 * Get the post_excerpt field
 * If post_excerpt is not set or the 'More' tag isn't used,
 * this will return the post_content field
**/
echo $post->getPostExcerpt()

// Get the post_content field
echo $post->getPostContent();

/**
 * Get collections of terms (eg. categories and tags) for $post
 * Custom taxonomies, require the Post Types & Taxonomies add-on
 * http://fishpig.co.uk/magento/wordpress-integration/post-types-taxonomies/
**/
// Get parent categories
$categories = $post->getTermCollection('category');

// An easier way to get the categories
$categories = $post->getParentCategories();

// Get the tags for $post
$tags = $post->getTermCollection('post_tag');

// Get a terms for $post from a custom taxonomy
// Where the custom taxonomy is 'ingredient'
$terms = $post->getTermCollection('ingredient');

Post Collections (including Pages and custom post types)

To make use of custom post types, you will need to install the Post Types & Taxonomies add-on extension for Magento.

 // Load a base collection of posts
 $posts = Mage::getResourceModel('wordpress/post_collection');
 
 // Print the SQL used in the collection to the screen
 $posts->load(true);exit;
 
// Filter the posts by a single post type
$posts->addPostTypeFilter('page');

/**
 * Filter the posts by multiple post types
 * If one of the post types is a custom post type
 * you will need the Post Types & Taxonomies add-on
 * http://fishpig.co.uk/magento/wordpress-integration/post-types-taxonomies/
**/
$posts->addPostTypeFilter(array('page', 'post', 'question'));

// Filter the posts by a single category (ID=4)
$posts->addTermIdFilter(4, 'category');

// Filter the posts by multiple category IDs (IDS=4, 5 and 6)
$posts->addTermIdFilter(array(4, 5), 'category');

// Filter the posts by multiple IDs of 
// a custom taxonomy (where custom taxonomy is ingredient)
$posts->addTermIdFilter(array(4, 5), 'ingredient');

// Filter posts by multiple post types and category IDs
$posts->addPostTypeFilter(array('page', 'post', 'question'))
	->addTermIdFilter(array(4, 5), 'category');

/**
 * Filter the posts by a term field. This can be the ID
 * but can be other fields such as name or slug
**/
// Return posts that are in the category with 'uncategorized' as its slug
$posts->addTermFilter('uncategorized', 'category', 'slug');

// Return posts that are in the category with 'Uncategorized' as its name
$posts->addTermFilter('Uncategorized', 'category', 'name');

// Add a parent ID filter. Useful for Pages
$posts->addPostParentIdFilter(96);

/**
 * Add the is_sticky field to the collection objects
 * You can then call $post->isSticky() on the individual 
 * post objects
**/
$posts->addStickyPostsToCollection();

/**
 * Filter the posts so that viewable posts are returned
 * Viewable means the post is published and takes into account
 * the protected and private status'
**/
$posts->addIsViewableFilter();

Term Models (eg. Categories, Tags & Custom Taxonomies)

In WordPress, categories and tags are stored using the term and taxonomy system. The taxonomy part represents the type of data and each data record is stored as a term. Using categories as an example, the taxonomy type is 'category' and the individual categories are all terms. By default, the two main taxonomy types in WordPress are 'category' (ie. post categories) and 'post_tag' (ie. post tags) but you can create your own.

In Magento WordPress Integration, all terms are represented by the Fishpig_Wordpress_Model_Term class and have their 'taxonomy' field set accordingly.

To make use of custom taxonomies, you will need to install the Post Types & Taxonomies add-on extension for Magento.

// Load a category with the ID of 1
$term = Mage::getModel('wordpress/term')->setTaxonomy('category')->load(1);

// Load a tag with the ID of 1
$term = Mage::getModel('wordpress/term')->setTaxonomy('post_tag')->load(1);

// Get the current taxonomy type
echo $term->getTaxonomy();

// Get the taxonomy label (eg. Categories or Tags)
echo $term->getTaxonomyLabel();

// Get the parent term. Returns false if no parent
$parentTerm = $term->getParentTerm();

/**
 * Get a collection of child terms
 * If $term was a category, this would return all subcategories
**/
$childTerms = $term->getChildrenTerms();

/**
 * Get a collection of posts belonging to $term
 * If $term was a category, this would return all posts 
 * in that category
**/
$posts = $term->getPostCollection();

// Get the number of posts that belong to $term
echo $term->getItemCount();

/**
 * Get the parent ID of the term
 * If $term was a category, this would be the parent category ID
**/
echo $term->getParentId();

// Get the term URL
echo $term->getUrl();

Term Collections (eg. Categories, Tags & Custom Taxonomy Collections)

To make use of custom taxonomies, you will need to install the Post Types & Taxonomies add-on extension for Magento.

 // Load a base collection of posts
 $terms = Mage::getResourceModel('wordpress/term_collection');
 
 // Print the SQL used in the collection to the screen
 $terms->load(true);exit;

// Filter the collection by slug
$terms->addSlugFilter('some_slug');

// Filter the collection by a taxonomy
$terms->addTaxonomyFilter('category');

// Filter the collection by a parent ID
$terms->addParentIdFilter(93); 

/**
 * Filter the collection by a post ID
 * This returns all terms that the post belongs to
 * This could be used to get all categories a post is in or tags if
 * you also called addTaxonomyFilter
**/
$terms->addPostIdFilter(95);

Is there anything we have missed? Please get in touch and let us know!

Post your comment

FishPig Ltd