Title: get_permalink not rendering correctly
Last modified: August 21, 2016

---

# get_permalink not rendering correctly

 *  [Justin Cline](https://wordpress.org/support/users/ryugraphix/)
 * (@ryugraphix)
 * [12 years, 6 months ago](https://wordpress.org/support/topic/get_permalink-not-rendering-correctly/)
 * I’m using a custom post type of “story” and pulling a random one to the home 
   page (front-page.php).
 * My WP_Query() is pulling all the other information correctly except for get_permalink().
   This is just printing “[http://domain.com/story/%story%/&#8221](http://domain.com/story/%story%/&#8221);
   instead of say “[http://domain.com/story/vanna-white/&#8221](http://domain.com/story/vanna-white/&#8221);.
 * Strangely it IS displaying properly in the admin on the actual post. I can click“
   View Story” next to it and display the page correctly.

Viewing 7 replies - 1 through 7 (of 7 total)

 *  [BeardedGinger](https://wordpress.org/support/users/joshlimecuda/)
 * (@joshlimecuda)
 * [12 years, 6 months ago](https://wordpress.org/support/topic/get_permalink-not-rendering-correctly/#post-4353256)
 * Hey Justin,
 * Does it give you the same output if you switch to using “the_permalink()” approach?
 * [http://codex.wordpress.org/Function_Reference/the_permalink](http://codex.wordpress.org/Function_Reference/the_permalink)
 *  Thread Starter [Justin Cline](https://wordpress.org/support/users/ryugraphix/)
 * (@ryugraphix)
 * [12 years, 6 months ago](https://wordpress.org/support/topic/get_permalink-not-rendering-correctly/#post-4353259)
 * Thanks for the reply! It does… prints exactly the same issue
 *  [BeardedGinger](https://wordpress.org/support/users/joshlimecuda/)
 * (@joshlimecuda)
 * [12 years, 6 months ago](https://wordpress.org/support/topic/get_permalink-not-rendering-correctly/#post-4353260)
 * Could you share the code you’re using to display the content on Front Page as
   well as the code used to create the custom post type?
 *  Thread Starter [Justin Cline](https://wordpress.org/support/users/ryugraphix/)
 * (@ryugraphix)
 * [12 years, 6 months ago](https://wordpress.org/support/topic/get_permalink-not-rendering-correctly/#post-4353263)
 * Custom post creation:
 *     ```
       /**
        * Custom Post Type: Stories
        *
        */
       	add_action('init', 'rr_story_register', 0);			// Register custom post type
   
        	function rr_story_register() {
       		$labels = array(
       			'name' 					=> _x('Client Stories', 'post type general name'),
       			'singular_name' 		=> _x('Story', 'post type singular name'),
       			'all_items' 			=> 'All Stories',
       			'add_new' 				=> _x('Add New', 'story'),
       			'add_new_item' 			=> __('Add New Story'),
       			'edit_item' 			=> __('Edit Story'),
       			'new_item' 				=> __('New Story'),
       			'view_item' 			=> __('View Story'),
       			'search_items' 			=> __('Search Stories'),
       			'not_found' 			=> __('Nothing found'),
       			'not_found_in_trash'	=> __('Nothing found in Trash'),
       			'parent_item_colon' 	=> ''
       		);
       		$args = array(
       			'labels' 				=> $labels,
       			'public' 				=> true,
       			'publicly_queryable' 	=> true,
       			'exclude_from_search' 	=> false,
       			'show_ui' 				=> true,
       			'query_var' 			=> true,
       			'rewrite' 				=> array( "slug" => "story"),
       			'capability_type' 		=> 'post',
       			'hierarchical' 			=> false,
       			'menu_position' 		=> 9,
       			'supports' 				=> array( 'title', 'editor', 'thumbnail', 'excerpt', 'revisions')
       		  );
       		register_post_type( 'story' , $args );
       		//flush_rewrite_rules( );
       	}
       ```
   
 * and the Front Page display:
 *     ```
       if ( ! function_exists( 'display_random_story_post' ) ) :
       /**
        * Displays latest blog post on the home page.
        * First post will show excerpt/phone, rest will just be a list.
        *
        * @return null
        */
       function display_random_story_post() {
           // Setup the Query
           $args = array (
               'post_type' => 'story',
               'posts_per_page' => 1,
               'orderby' => 'rand'
           );
           $rand_story = new WP_Query( $args );
   
            // Setup var
           $display = null;
   
           //foreach ( $rand_story as $story ) : setup_postdata( $story );
           if($rand_story->have_posts()) :
   
               while($rand_story->have_posts()) : $rand_story->the_post();
   
                   $client_name        = get_post_meta( get_the_ID(), 'rr_story_name', true);
                   $client_location    = get_post_meta( get_the_ID(), 'rr_story_location', true);
                   $page_headline      = get_post_meta( get_the_ID(), 'rr_story_headline', true);
                   $use_alt_headline   = 1;
   
                   if ($page_headline == '') {
                       $use_alt_headline = 0;
                       $page_headline = get_the_title();
                   }
   
                   $display    = '<article id="post-' .get_the_ID(). '" class="latest-post">' . "\r\n";
                   $display    .= '<header>' . "\r\n";
                   $display    .= '<h2><a href="' .get_permalink(). '">' .$page_headline. '</a></h2>' . "\r\n";
                   if( $use_alt_headline == 1 && $client_location !== '') {
                       $display    .= '<div class="entry-meta"><p>' .$client_name. ', ' .$client_location. '</p></div>';
                   } elseif( $use_alt_headline == 1 ) {
                       $display    .= '<div class="entry-meta"><p>' .$client_name. '</p></div>';
                   } else {
                       $display    .= '<div class="entry-meta"><p>' .$client_location. '</p></div>';
                   }
                   $display    .= '</header>' . "\r\n";
                   $display    .= return_excerpt(280);
                   $display .= '</article><!-- END #post-' .get_the_ID(). ' -->' . "\r\n";
               endwhile; // End post display loop
   
           else: // If there are no posts, say so
   
               $display .= '<p>We're sorry, there are no stories to display.</p>';
   
           endif; // End loop to get blog posts
   
           echo $display;
   
           wp_reset_postdata();
   
       } // END display_random_story_post()
   
       endif;
       ```
   
 *  [BeardedGinger](https://wordpress.org/support/users/joshlimecuda/)
 * (@joshlimecuda)
 * [12 years, 6 months ago](https://wordpress.org/support/topic/get_permalink-not-rendering-correctly/#post-4353365)
 * What happens when you change the query to pull `'post_type' => 'post'` or `'post_type'
   => 'page'`? Does it still cause the same url issue?
 *  Thread Starter [Justin Cline](https://wordpress.org/support/users/ryugraphix/)
 * (@ryugraphix)
 * [12 years, 6 months ago](https://wordpress.org/support/topic/get_permalink-not-rendering-correctly/#post-4353366)
 * Hmm, looks like those pull correctly. Out of curiosity I changed to one of the
   two other custom post types I’m using… the “events” one worked ok, but not another
   using the basic method of custom post permalinks.
 * The one that’s working is “Events” and it set to rewrite => false, but then I
   have a custom function I’d found someplace, that’s making it use ID. I’m guessing
   this is disrupting the rest of the custom posts’ rewriting…
 *     ```
       add_action('init', 'rr_event_rewrite');
       function rr_event_rewrite() {
           global $wp_rewrite;
       	$queryarg = 'post_type=event&p=';
           $wp_rewrite->add_rewrite_tag('%event_id%', '([^/]+)',$queryarg);
           $wp_rewrite->add_permastruct('event', '/event/%event_id%', false);
           //$wp_rewrite->flush_rules();
       }
   
       add_filter('post_type_link', 'rr_event_permalink', 1, 3);
       function rr_event_permalink($post_link, $id = 0, $leavename) {
           global $wp_rewrite;
           $post = get_post($id);
           if ( is_wp_error( $post ) )
       	return $post;
           $newlink = $wp_rewrite->get_extra_permastruct($post->post_type);
           $newlink = str_replace('%'.$post->post_type.'_id%', $post->ID, $newlink);
           $newlink = home_url(user_trailingslashit($newlink));
           return $newlink;
       }
       ```
   
 * Don’t know enough about $wp_rewrite to trouble shoot, but I’m guessing it doesn’t
   play well with others.
 * Thanks so much for all the help up to this point, I really appreciate it!
 *  Thread Starter [Justin Cline](https://wordpress.org/support/users/ryugraphix/)
 * (@ryugraphix)
 * [12 years, 6 months ago](https://wordpress.org/support/topic/get_permalink-not-rendering-correctly/#post-4353394)
 * I’m just bailing and using a plugin
 * ([http://wordpress.org/support/plugin/wp-permastructure](http://wordpress.org/support/plugin/wp-permastructure))
 * Working now well enough now, thanks though!

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘get_permalink not rendering correctly’ is closed to new replies.

## Tags

 * [custom post](https://wordpress.org/support/topic-tag/custom-post/)
 * [get_permalink](https://wordpress.org/support/topic-tag/get_permalink/)
 * [permalink](https://wordpress.org/support/topic-tag/permalink/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 2 participants
 * Last reply from: [Justin Cline](https://wordpress.org/support/users/ryugraphix/)
 * Last activity: [12 years, 6 months ago](https://wordpress.org/support/topic/get_permalink-not-rendering-correctly/#post-4353394)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
