So let's say you have a basic storefront set up with WordPress. You might want to have a brand page that details information about that brand. In your store you have multiple categories of products. For example: hats, tee-shirts, mugs, etc. WordPress posts already have the category taxonomy built in (duh). So let's say it would be cool to have a page that is a parent to a set of posts. Brand X makes hats, and mugs Brand Y makes tee-shirts and mugs It would be nice to be able to pull all of the products that Brand X makes onto their page. What this plugin does is the following: 1) Places a meta box onto all pages with the following question. Is this a post parent? We want to specify this as we don't care about an About Us page or Contact Us page for these purposes 2) Then on each post, there is an jquery-ui autocomplete box at the top right that allows you to start typing and then select a page that has been marked as being a parent page. You can modify this to have a dropdown select menu of the possible parents. I built it this way to have some flair, but also to accommodate a store with a ton of pages that would make the select menu too long to manage 3) When you click publish/update on the post, WordPress saves the page parent as a meta key/value pair. So...now you can do a query on your page.php or any page template where you can declare the following: Show all posts that have a page parent with Brand X. This aggregates all the company's posts onto a page as an overview of their products. To implement it you can use "get_posts();" on a page template
<?php
// the $post->title is the title of the page. I am working on a version that bases this on the ID as that doesn't change. but the concept will be the same
$args = array(
"meta_key" => "_lptp_page_association",
"meta_value" => $post->post_title
);
$brand_x_posts = get_posts($args);
foreach ($brand_x_posts as $brand_x_post) {
echo $brand_x_post->post_title;
// this will echo out the title of the post (product in this example). You can echo out anything from the wp_posts table at this point for display.
}
?>
Download_Source_Files.zip
<?php
/*
Plugin Name: Link Posts To Pages
Plugin URI:
Description: This plugin hooks up posts to pages
Version: 1.0
Author: Matthew Price
Author URI: http://www.matthewaprice.com
License: GPL2
*/
// This gets all of the pages that have been assigned as "yes I am a page parent"
function lptp_get_pages_auto_complete() {
global $wpdb;
global $post;
$pgs = $wpdb->get_results(" SELECT ID,post_title,post_status,post_type
FROM {$wpdb->prefix}posts
WHERE post_type = 'page'
AND post_status = 'publish'
");
$current_association = get_post_meta($post->ID,'_lptp_page_association',true);
foreach ($pgs as $pg) {
$parent = get_post_meta($pg->ID,'_lptp_is_page_post_parent',true);
if ($parent == '1') {
?>'<?php echo $pg->post_title; ?>',<?php
}
}
}
// This creates the javascript on the add/edit post page for the autocomplete functionality.
function lptp_display_pages_auto_complete_on_post() {
global $post;
$current_association = get_post_meta($post->ID,'_lptp_page_association',true);
?>
<script type="text/javascript">
jQuery(document).ready(function(){
var availableTags = [<?php lptp_get_pages_auto_complete(); ?>];
jQuery('#page_assoc').autocomplete({
source: availableTags
});
});
</script>
<input type="hidden" name="old_page_assoc" id="old_page_assoc" value="<?php echo $current_association; ?>">
<input name="page_assoc" id="page_assoc" style="width: 225px;" value="<?php echo $current_association; ?>">
<?php
}
// This addes the previous function as a meta box to posts
function lptp_add_page_association_box_to_post( $page, $context ) {
if ( ( 'post' === $page ) && 'advanced' === $context )
add_meta_box( 'page-association', 'Page Association', 'lptp_display_pages_auto_complete_on_post', $page, 'side', 'high' );
}
// This saves the page/post parent relationship to the db in wp_postmeta
function lptp_save_page_association_to_db($postID) {
// this makes sure that the custom postmeta is not wiped out during an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// this makes sure that this only runs on the post editor. the ajax quick edit will nuke the custom postmeta as well
if ( ( isset ($_POST['save'] ) ) || ( isset( $_POST['publish'] ) ) ) :
$old_page_association = $_POST['old_page_assoc'];
$new_page_association = $_POST['page_assoc'];
update_post_meta($postID, '_lptp_page_association', $new_page_association, $old_page_association);
endif;
}
// These are wp actions that call the two previous functions
add_action( 'do_meta_boxes', 'lptp_add_page_association_box_to_post', 10, 2 );
add_action( 'save_post', 'lptp_save_page_association_to_db');
// This is the question on a page that asks "Is this a parent?"
function lptp_identify_page_as_post_parent() {
global $post;
$parent = get_post_meta($post->ID,'_lptp_is_page_post_parent',true);
?>
<label for="parentYes">Yes<input type="radio" name="parent" id="parentYes" value="1"<?php if ($parent == '1') { echo ' checked="checked"'; } ?>></label>
<label for="parentNo">No<input type="radio" name="parent" id="parentNo" value="0"<?php if ($parent == '0') { echo ' checked="checked"'; } ?>></label>
<?php
}
// This adds the previous function as a meta box on add/edit pages
function lptp_add_identify_page_as_parent_box( $page, $context ) {
if ( ( 'page' === $page ) && 'advanced' === $context )
add_meta_box( 'parent-box', 'Is this a Post Parent?', 'lptp_identify_page_as_post_parent', $page, 'side', 'high' );
}
// This saves the answer to the page parent question to the db in wp_postmeta
function lptp_save_page_as_post_parent_to_db($postID) {
global $wpdb;
$parent = $_POST['parent'];
if ($parent == '') {
update_post_meta($postID, '_lptp_is_page_post_parent', '0');
} else {
update_post_meta($postID, '_lptp_is_page_post_parent', $parent);
}
}
// These are the wp actions that call the two previous functions
add_action( 'do_meta_boxes', 'lptp_add_identify_page_as_parent_box', 10, 2 );
add_action( 'save_post', 'lptp_save_page_as_post_parent_to_db');
// This registers the jquery-ui autocomplete functionality
wp_register_script( 'jquery-ui-autocomplete', WP_PLUGIN_URL . '/link-posts-to-pages/js/jquery-ui-1.8.10.custom.min.js' );
wp_enqueue_script( 'jquery-ui-autocomplete' );
?>