How to Link Posts to Pages in WordPress

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

// 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.  

Plugin Name: Link Posts To Pages
Plugin URI:
Description: This plugin hooks up posts to pages
Version: 1.0
Author: Matthew Price
Author URI:
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">

        var availableTags = [<?php lptp_get_pages_auto_complete(); ?>];
                source: availableTags


<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; ?>">



// 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);



// 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>


// 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' );

Post a comment
  1. Muhammad Usama

    Hi Matthew, Thank for your unique plugin. I would like to display this plugin option at custom post type. This time it show at default page. How it would possible?

    • Matthew Price

      Hello, This can be very easily done. The function named "lptp_add_page_association_box_to_post" has a conditional for "if ( ( 'post' === $page )" You can change this to "if ( ( 'custom_post_type' === $page )" That will add the association box to your custom post type Matt

      • Muhammad Usama

        HI Matthew, I am facing an issue that every time that when I assign an custom post to page. It showing the custom post on page but after 1min the value of meta_key will be reset and it become 0 in database. I am unable to find the issue, please suggest some working solution.

        • Matthew Price

          Hi I have updated the function that saves the postmeta. It needed the check for autosave and the correct submit buttons. Matt

        • Muhammad Usama

          I have comment jquery file and now it working perfectly alright. But this is not an solution for problem.

          • Matthew Price

            Check out my response to your previous question. this code was written before i had the extra code in there every time i had an action on save_post.

      • Muhammad Usama

        Excellent Matthew. Its working.

        • Matthew Price

          Cool! Glad to be able to help

Real Time Web Analytics ^