logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$75
Custom post type permalinks all load the homepage

code here: http://pastebin.com/MhQAj9H3

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!

This question has been answered.

jsros86 | 05/25/12 at 11:33am Edit

Previous versions of this question: 05/25/12 at 2:08pm

(8) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    05/25/12
    11:48am
    Francisco Javier Carazo Gil says:

    Try to flush rules:


    function my_em_rewrite_flush(){
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
    }
    add_action('init','my_em_rewrite_flush');

  • avatar
    Last edited:
    05/25/12
    11:48am
    Ross Wilson says:

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

  • avatar
    Last edited:
    05/25/12
    11:49am
    Peter Michael says:

    Are your new rewrite rules correct & at the top when you check it with http://wordpress.org/extend/plugins/askapaches-rewriterules-viewer/ ?

  • avatar
    Last edited:
    05/25/12
    11:55am
    John Cotton says:

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


    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->init();
    $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 :)

    • 05/25/12 12:27pm

      jsros86 says:

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

    • 05/25/12 12:30pm

      John Cotton says:

      Sorry, should be

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

    • 05/25/12 12:42pm

      jsros86 says:

      still showing the homepage :(

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

    • 05/25/12 2:08pm

      jsros86 says:

      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->init();

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

      }

      add_filter( 'pre_get_posts', 'set_property_id' );

    • 05/25/12 2:18pm

      John Cotton says:

      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.

    • 05/25/12 2:21pm

      jsros86 says:

      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!

    • 05/25/12 2:24pm

      John Cotton says:


      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->init();
      query->query = array('id' => $property_id, 'post_type' => 'property');
      }
      }
      add_filter( 'pre_get_posts', 'set_property_id' );

    • 05/25/12 2:37pm

      jsros86 says:

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

    • 05/25/12 2:41pm

      John Cotton says:

      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:

      ini_set('memory_limit','256M');


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

  • avatar
    Last edited:
    05/25/12
    12:13pm
    Luis Abarca says:

    Im using this to have slug with ID like domain.com/property/1234

    http://pastebin.com/PiJXVenN

  • avatar
    Last edited:
    05/25/12
    1:07pm
    Arnav Joy says:

    try creating a new php file and name it as
    single-property.php

    use the code which is at single.php

  • avatar
    Last edited:
    05/25/12
    5:06pm
    AdamGold says:

    First of all, I highly recommend using the following plugin to analyze your URLs:
    http://wordpress.org/extend/plugins/monkeyman-rewrite-analyzer/


    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:
    http://example.com/property/SOME_PROPERTY_ID

    then please make a new php file with the following:

    <?php
    /*
    Template Name: Property
    */
    get_header();
    $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> / Location #<?php echo $post->ID?><?php echo (isset($galpage) ? ': Page '.$galpage : ''); ?>
    </header>

    <a class="sidebar-expand icon-button"></a>
    </div>
    <?php
    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') );


    query_posts($args);
    ?>
    <?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>
    <?php
    $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>
    </div>
    <?php
    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>
    </div>

    <?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>
    </div>
    </div> <!-- sidebar -->

    <div class="content clearfix">

    <?php the_content(); ?>

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




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

    Good luck!

    Previous versions of this answer: 05/25/12 at 2:34pm | 05/25/12 at 2:36pm | 05/25/12 at 2:47pm | 05/25/12 at 5:06pm

  • avatar
    Last edited:
    05/25/12
    4:07pm
    Gabriel Reguly says:

    Hi,

    What does your links look like?

    http://example.com/property/1

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

    I have done this site http://imobiliariamaggiore.com.br/imoveis/co-02151/ 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.


    Regards,
    Gabriel

    • 05/25/12 4:09pm

      jsros86 says:

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

    • 05/25/12 4:28pm

      Gabriel Reguly says:

      Hi,

      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.

      Regards,
      Gabriel

This question has expired.



Gabriel Reguly, Arnav Joy, jsros86 voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.