Permalink plays an important role in SEO practices. Search engines look for keywords in URL also. This post is not about detailing permalinks. In this post, I have told you the method to change the permalink for wp posts and custom post types in entry contents.

The reason for writing this post is very simple. When I worked on a project on wordpress, I need to display all the post types in the main page, search page and archives as well.

BlueHost

But the problem is the custom post types have a custom field which store external link and this external link replace the default URL of the custom post type. Means when someone clicks on custom post types entry content, It redirects the user to an external link which is stored by a custom field.

Issue 1 – Displaying Post Types and Custom Post Types on blog Page, archive page, search page etc.

Issue 2 – Set custom field source URL for custom post types (replacing default URL by Custom Field URL)

Get Started Step By Step

Now we started. lets we set a name for custom post types, for example, my custom post type is ‘Books’.

For Example : custom_post_type => 'Book'

Issue 1 – Displaying Custom Post Types ALong With Posts

Displaying Custom Post Types In Main or Blog Page, Archive Page, Search Result along with main WordPress Posts Types.

You Just need to filter pre_get_posts to achieve this task.  For this work here is a piece of code you need to add to functions.php file of your wordpress theme. You can read more about it here on WP Codex

add_action( 'pre_get_posts', 'wpy_display_book_in_blog' );
function wpy_display_book_in_blog( $query ) {
if (is_home() || is_single() || is_admin()) {
return;
}

if ( $query->is_main_query() ) :
$query->set( 'post_type', array('post', 'book') );
endif;
}

is_home()  is compulsory if your home page is a custom page instead of regular blog posts.

is_single() is compulsory to prevent any error to the single pages and posts. (a common error is 404 page not found).

is_admin() is compulsory to mashup wordpress admin area. (posts and pages, custom post types )

if you are using front-page like modern wordpress theme comes with it, you must use is_front_page() along with others as in the example.

10+ Essential Tools Must For Every Professional Website

Setup Blog Checklist – Facts Behind A Successful Blog

This will exactly solve your problem and you will be able to display custom post types along with regular posts in all pages like the blog page, main page, archive and even search result page.

Issue 2 – Replacing the URL of custom post types using the custom field.

From the above image, you can clearly understand what I am trying to explain. Regular Post types have default site URL but the custom post types have external URL replacing default site URL.

Step 1 – Store the URL like the very first image using the custom field. to achieve this goal, I need to create a custom field and name anything according to your need example, my_external_url, store value in it when you write a custom post and want to link it to external sources.

Step 2 – Add a piece of code in functions.php file in your theme. filtering the the_permalink() function only.

function themename_desired_url() {

global$post;

if ($post->post_type=="book") {
echo ''.esc_url( get_post_meta( get_the_ID(), 'nw_notes_source_url', true ) ) .'';

} 
else 
{ 
return get_the_permalink(); 
} 
} 
add_filter( 'the_permalink', 'themename_desired_url' );
Now You have done, but the default permalink can still redirect you to the single posts page. To prevent it you need to add another piece of code in functions.php file in your theme.
add_action( 'template_redirect', 'themename_singular_view_prevention' );

function themename_singular_view_prevention() {

$queried_post_type=get_query_var('post_type');

if ( is_single() &&'book'==$queried_post_type ) {

wp_redirect( get_post_type_archive_link( 'book' ), 301 ); // important line!

exit;

}

}

301 redirect is the permanent redirect. you can also use 302 redirects for the temporary redirect.

wp_redirect( get_post_type_archive_link( ‘book’ ), 301 ); it will redirect you to the custom post type archieve page.

if you want to redirect you to the home page, just use

wp_redirect(home_url(), 301);

want to hide it in navigation menus and exclude from search just use

'show_in_nav_menus' => false,  // if not, then omit or set true
'exclude_from_search' => true  // if not, then omit or set false

You can set more parameter according to your need. you can find the parameter for custom post types here in codex page.

let me know if you have any issue or other tricks to achieve this goal. For any assistance feel free to leave a message in the comment box.

Reference – WP Codex Page , Featured Image – Pixabay

WP Yard

WP Yard - A place where you can read about WordPress and Blogging. You can also troubleshoot your site issue (You only need to connect via contact form). Join our Social media platform and help us to grow!

This Post Has One Comment

  1. Hello ,

    I saw your tweets and thought I will check your website. Have to say it looks very good!
    I’m also interested in this topic and have recently started my journey as young entrepreneur.

    I’m also looking for the ways on how to promote my website. I have tried AdSense and Facebok Ads, however it is getting very expensive.
    Can you recommend something what works best for you?

    Would appreciate, if you can have a quick look at my website and give me an advice what I should improve: http://janzac.com/
    (Recently I have added a new page about FutureNet and the way how users can make money on this social networking portal.)

    I have subscribed to your newsletter. 🙂

    Hope to hear from you soon.

    P.S.
    Maybe I will add link to your website on my website and you will add link to my website on your website? It will improve SEO of our websites, right? What do you think?

    Regards
    Jan Zac

Leave a Reply

%d bloggers like this: