Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom post type permalinks all load the homepage WordPress


code here:

I'm trying to create permalinks for a custom post type, "property" based on a custom field value, "the_property_id".

This custom field value corresponds to the post count, so the first published post has a "the_property_id" of 1, and the 160th post has a "the_property_id" value 160.

The following code successfully creates the permalinks, but when you actually click the property link to a single page, it loads the homepage rather than the indicated single page.

I've refreshed the permalinks whenever I save this function, and have tried every troubleshooting suggested by several google searches...

What am I doing wrong here? Thanks!

Answers (8)


AdamGold answers:

First of all, I highly recommend using the following plugin to analyze your URLs:

Second, please add:

function create_rewrite_rules($rules) {
   global $wp_rewrite;
   $newRule = array('property/(.+)/?$' => 'index.php?pagename=property&the_property_id=' . $wp_rewrite->preg_index(1));
   $newRules = $newRule + $rules;
   return $newRules;

function add_query_vars($qvars) {
   $qvars[] = 'the_property_id';
   return $qvars;

add_filter('rewrite_rules_array', 'create_rewrite_rules');
add_filter('query_vars', 'add_query_vars');

This will create the URL:

then please make a new php file with the following:

Template Name: Property
$galpage = $_GET['galleryPage'];

$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'posts_per_page' => -1,
'post_parent' => $post->ID
$attachments = get_posts($args);
$img_count = count($attachments);

<div class="labels locations-labels">
<header class="breadcrumbs">
<a href="<?php bloginfo('url'); ?>/">Home</a> / <a href="<?php bloginfo('url'); ?>/locations">Locations</a> / <strong>Location #<?php echo $post->ID?><?php echo (isset($galpage) ? ': Page '.$galpage : ''); ?></strong>

<a class="sidebar-expand icon-button"></a>
global $wp_query;
$args = array('post_type' => 'property', 'posts_per_page' => 1, 'meta_key' => 'the_property_id', 'meta_value' => $wp_query->get('the_property_id') );

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<div class="property-sidebar col-280 sidebar clearfix">

<h1 class="property-title terminal dash-bottom">Location #<?php the_ID(); ?></h1>
<p class="dash-bottom"><?php echo $img_count; ?> Images</p>
<a target="_blank" class="icon-button dash-bottom download" href="<?php bloginfo('wpurl'); ?>/create-pdf?location_id=<?php echo the_ID(); ?>">Download Location PDF</a>

<div class="clear"></div>

<?php if (has_access()) { $property_desc = get('property_description'); if($property_desc) { ?>

<p class="property-desc dash-bottom"><?php echo strip_tags($property_desc); ?></p>
<?php } } ?>

<div class="tags dash-bottom">
<b class="clearfix">Tags:</b>
$area = get_the_terms(get_the_ID(),'area');
$type = get_the_terms(get_the_ID(),'property_type');
$style = get_the_terms(get_the_ID(),'style');
$feature = get_the_terms(get_the_ID(),'feature');

if(!$area) {$area = array();}
if(!$type) {$type = array();}
if(!$style) {$style = array();}
if(!$feature) {$feature = array();}

$tags = array_merge($area, $type, $style, $feature);
foreach($tags as $tag) {
echo '<a class="tag">' . $tag->name . '</a>';
$contents = $jcart->get_contents();
$contents = count($contents);

$property_info = array(
"Address" => "property_address",
"City" => "property_city",
"State" => "property_state",
"Zip" => "property_zip",
"Square Feet" => "property_sqft",
"Lot Square Feet" => "property_lot_sqft",
"Year" => "property_year",
"Levels" => "property_levels",
"Bedrooms" => "property_bedrooms",
"Baths" => "property_baths",
"Garage" => "property_garage",
"Stairs" => "property_stairs",
"Kids" => "property_kids",
"Pets" => "property_pets",
"Gated" => "property_gated",
"Gated Contact" => "property_gated_contact",
"Owner first" => "property_owner_first",
"Owner last" => "property_owner_last",
"Payment name" => "property_payment_name",
"Email" => "property_email",
"Phone" => "property_phone",
"Cell" => "property_cell",
"Fax" => "property_fax",
"Mailing" => "property_mailing",
"Mailing Address" => "property_mailing_address",
"Mailing City" => "property_mailing_city",
"Mailing State" => "property_mailing_state",
"Mailing Zip" => "property_mailing_zip",
"Best to reach" => "property_best",
"Filming" => "property_filming",
"Property Budget" => "property_budget",
"Frequency" => "property_frequency",
"Cleaning" => "property_cleaning",
"Cleaning Info" => "property_cleaning_info",
"Construction nearby" => "property_construction",
"Parking" => "property_parking",
"Facing" => "property_facing",

<div class="clear"></div>
if (has_access()) {
foreach($property_info as $key => $value) {
$value = get_post_meta(get_the_ID(), $value, true);
if($value) {
echo $key.': '.$value.'</br>';
<div id="sidebar-pullsheet"<?php if($contents == 0) {echo ' class="ps-empty"';}?>>
<div id="ps-help">
<a title="What's This?"></a>
<p class="dash-bottom">The Pullsheet is your personal collection of images to be saved as a PDF file.You can add individual images from any of our Locations to your Pullsheet or you can download the complete set of images from individual Locations as a PDF file.</p>

<?php $jcart->display_cart();?>
<strong class="small-caps dash-bottom view-wrap">
<a href="<?php bloginfo('wpurl'); ?>/view-pullsheet">Edit Pullsheet</a>

<a target="_blank" href="<?php bloginfo('wpurl'); ?>/create-pdf" class="jcart-checkout no-ajaxy terminal jcart-button action">Download PDF</a>
<a href="<?php bloginfo('wpurl'); ?>/create-pdf?store=true" class="jcart-checkout no-ajaxy terminal jcart-button action share-pdf">Share PDF</a>
<div id="share-url">
<input type="text" />
<p class="caps block">Copy the URL above to email or share</p>
</div> <!-- sidebar -->

<div class="content clearfix">

<?php the_content(); ?>

<?php endwhile; endif; ?>
<?php get_footer(); ?>

now, make a new page with "Property" as custom page template.

Good luck!


Francisco Javier Carazo Gil answers:

Try to flush rules:

function my_em_rewrite_flush(){
global $wp_rewrite;


Ross Wilson answers:

Do you have a link you can share with us? What do your permalinks look like when they are output as html?


Peter Michael answers:

Are your new rewrite rules correct & at the top when you check it with [[LINK href=""]][[/LINK]] ?


John Cotton answers:

<blockquote>but when you actually click the property link to a single page, it loads the homepage rather than the indicated single page.</blockquote>

That's not surprising since WP has no way of knowing which page to load. Creating a link that WP responds to isn't the same as telling it which page to load.

I take it that the_property_id is unique across the posts?

If so, you need to set the post id in the query. Try adding this code:

function set_property_id( $query ){

if( $prop_id = get_query_var('post_custom_data') ) {
$property = new WP_Query( array( 'meta_key' => 'the_property_id', 'meta_value' => $prop_id ) );

$query->query( 'id' => $property->posts[0]->ID, 'post_type' => 'property' );
add_filter( 'pre_get_posts', 'set_property_id' );

I haven't tried that code, but it should work :)

jsros86 comments:

Thanks John!

I get the following error:
Parse error: syntax error, unexpected T_DOUBLE_ARROW

for this line:

` $query->query( 'id' => $property->posts[0]->ID, 'post_type' => 'property' );

John Cotton comments:

Sorry, should be

$query->query( array('id' => $property->posts[0]->ID, 'post_type' => 'property') );

jsros86 comments:

still showing the homepage :(

I flushed my rewrite rules as well after saving that function by going to the permalinks page and clicking save.

jsros86 comments:

I tried modifying it to this:

but I get "Fatal error: Allowed memory size of 103809024 bytes exhausted (tried to allocate 523800 bytes) in query.php line 2913.

function set_property_id( $query ){

if( strstr( $_SERVER['REQUEST_URI'], 'property/') ) {

$prop_id = explode('/',$_SERVER['REQUEST_URI']);
$prop_id = $prop_id[2];

$property = new WP_Query( array( 'meta_key' => 'the_property_id', 'meta_value' => $prop_id ) );


$query->query( array('id' => $property->posts[0]->ID, 'post_type' => 'property') );


add_filter( 'pre_get_posts', 'set_property_id' );

John Cotton comments:

Try changing the line to this:

$property_id = $property->posts[0]->ID;
echo 'pid = '. $property_id;
query->query = array('id' => $property_id, 'post_type' => 'property');

and check that the correct ID for the post you're trying to retrieve is output.

Also, I have assumed your post type is property. If that's not the case, you need to change that too.

jsros86 comments:

John could you please paste the entire function as I'm not sure if you're referencing the original or one of the variations we've posted. thanks!

John Cotton comments:

function set_property_id( $query ){
if( $prop_id = get_query_var('post_custom_data') ) {
$property = new WP_Query( array( 'meta_key' => 'the_property_id', 'meta_value' => $prop_id ) );
$property_id = $property->posts[0]->ID;
echo 'pid = '. $property_id;

query->query = array('id' => $property_id, 'post_type' => 'property');
add_filter( 'pre_get_posts', 'set_property_id' );

jsros86 comments:

That still gives me the memory allocation error... :(

John Cotton comments:

How much memory are you set to? That script shouldn't use so much...

Add this line at the top of your functions.php file:


That should be more than enough. If it works, bring down the level ( 128, 64 etc) and see where it breaks again.


Luis Abarca answers:

Im using this to have slug with ID like

[[LINK href=""]][[/LINK]]


Arnav Joy answers:

try creating a new php file and name it as

use the code which is at single.php


Gabriel Reguly answers:


What does your links look like?

What is your permalink selection? I use /%category%/%postname%/

I have done this site [[LINK href=""]][[/LINK]] and seems to me that it works pretty much the way you want.

Here is some of my code for it:

// Register our custom post type wp_imovel_property
$labels = array(
'name' => __( 'Properties', 'wp-imovel' ),
'singular_name' => __( 'Property', 'wp-imovel' ),
'add_new' => __( 'Add New', 'wp-imovel' ),
'add_new_item' => __( 'Add New Property', 'wp-imovel' ),
'edit' => __( 'Edit', 'wp-imovel' ),
'edit_item' => __( 'Edit Property', 'wp-imovel' ),
'new_item' => __( 'New Property', 'wp-imovel' ),
'view' => __( 'View', 'wp-imovel' ),
'view_item' => __( 'View Property', 'wp-imovel' ),
'search_items' => __( 'Search Properties', 'wp-imovel' ),
'not_found' => __( 'No properties found', 'wp-imovel' ),
'not_found_in_trash' => __( 'No properties found in Trash', 'wp-imovel' ),
'parent_item_colon' => ':'
$args = array(
'labels' => $labels,
'singular_label' => __( 'Property', 'wp-imovel' ),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'menu_position' => 2,
'hierarchical' => true,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'author', 'revisions' ),
'rewrite' => array( 'slug' => $wp_imovel['configuration']['base_slug'], 'with_front' => false ),
'query_var' => $wp_imovel['configuration']['base_slug'],
'menu_icon' => WP_IMOVEL_URL . '/images/wp-imovel-menu-icon.png',
'description' => __( 'Easily manage your properties here.', 'wp-imovel' ),
'has_archive' => 'vendido'
register_post_type( 'wp_imovel_property', $args );

That is just half of the trick, I'll post the rest here soon.


jsros86 comments:

If "co-02151" uses the $post->ID then it is *not* what I'm looking for.

My custom field is a sequential numbering of the posts...

Gabriel Reguly comments:


Sorry, but if you do not use $post->ID, how do you expect to identify the posts?

I think that your custom field must have the id somewhere.

In my "co-02151", "co" means penthouse and 2151 is the property id.

The slug co-02151 is meant to be more friendly than just a bare number, so prospective clients would have a fixed code to mention to the brokers.

Please correct me if I am wrong, but that is the first thing I would fix.