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

WP Rewrite rules for home/index page WordPress

  • SOLVED

I've been struggling for a while already and all of my attempts failed so I have no other choice but to ask for help here...

a) I've made a custom post type <strong>page_blox</strong> whose <strong>capability_type => 'page' </strong>and <strong>rewrite => array( 'slug' => 'blox' )</strong>

b) All of <strong>page_blox</strong> pages can be selected on <strong>Reading Settings</strong> screen (Admin) as 'Front page displays' > Static page

So far so good. However...

<strong>The problem:</strong>

Front-end home page URL doesn't read <strong>my_domain.com</strong> any more but also appends <strong>post_type</strong> and <strong>page slug</strong> so it looks like
<strong>my_domain.com?page_blox=my-page-slug</strong>

<strong>The Question:</strong>

I believe it has something to do with WP's rewrite rules so how do I remove that appendix (<strong>?page_blox=my-page-slug</strong>) from home page URL?

Answers (4)

2013-10-23

Remy answers:

What are your permalinks settings in the WP settings page ?


dameer comments:

/%category%/%postname%/

...but I guess it shouldn't make any difference if set to default structure as well


Remy comments:

Did you look at the answer in this question : [[LINK href="http://wpquestions.com/question/show/id/2944"]]http://wpquestions.com/question/show/id/2944[[/LINK]]


dameer comments:

Yes I did and it doesn't work. Do you have any suggestion on how to modify buildRewrites() ($newrules) for my custom post type home page?


Remy comments:

Here is something I tried on a local install and works


function enable_front_page_cpt( $query )
{
if('' == $query->query_vars['post_type'] && 0 != $query->query_vars['page_id'])
$query->query_vars['post_type'] = array( 'page', 'page_blox' );
}
add_action( 'pre_get_posts', 'enable_front_page_cpt' );


dameer comments:

Turn wp_debug on, there's an error : Undefined index: post_type in


Remy comments:

This will fix the notice

function enable_front_page_produit( $query )
{
if( !isset( $query->query_vars['post_type'] ) && 0 != $query->query_vars['page_id'])
$query->query_vars['post_type'] = array( 'page', 'produit' );
}
add_action( 'pre_get_posts', 'enable_front_page_produit' );


dameer comments:

No error but the question is: how does this affect home page rewrite rules when CPT is set as home page?
With permalinks set to "/%category%/%postname%/", my home page is still like:

http://localsite.lh/blox/front-page-a/

...while with default permalinks it reads:

http://localsite.lh/?page_blox=front-page-a

...regardless 'pre_get_posts'


Remy comments:

It's working fine even with your rewrite rules on my local install. I see that I c/c my own code directly and didn't adapt it for you on my last answer, here it is again :



function enable_front_page_blox( $query )

{

if( !isset( $query->query_vars['post_type'] ) && 0 != $query->query_vars['page_id'])

$query->query_vars['post_type'] = array( 'page', 'page_blox' );

}

add_action( 'pre_get_posts', 'enable_front_page_blox' );


dameer comments:

I'm sorry to say but it does not work for me. What I can see now is a blank page. Do you need any other code from my side?


Remy comments:

What is the function that is adding your posts in your cpt page_blox to the front page dropdown on the reading settings page ?


dameer comments:

Here it is:

function k_blox_to_dropdown( $select ) {
if( FALSE === strpos( $select, 'page_on_front' ) ) return $select;
$blox = get_posts( array( 'post_type' => 'page_blox', 'post_status' => 'publish', 'posts_per_page' => -1 ) );
if( !$blox ) return $select;

$page_on_front = intval( get_option( 'page_on_front' ) ) ? get_option( 'page_on_front' ) : -1;
$blox_options = walk_page_dropdown_tree(
$blox,
0,
array( 'depth' => 0, 'child_of' => 0, 'selected' => $page_on_front, 'echo' => 0, 'name' => 'page_on_front', 'id' => '', 'show_option_none' => '', 'show_option_no_change' => '', 'option_none_value' => '' )
);

return str_replace( '</select>', $blox_options . '</select>', $select );
}
add_filter( 'wp_dropdown_pages', 'k_blox_to_dropdown', 10, 1 );


dameer comments:

Almost forgot...
Please be sure that you have/create "single-page_blox.php" template in the testing theme folder!


Remy comments:

With this it will use either front-page.php if present, or page.php as the template


dameer comments:

That's why I asked how is affects home page rewrite rules when CPT is set as home page :)


Remy comments:

To use your single-page_blox.php template for the front page


function front_page_redirect()
{
if( is_front_page() )
{
include( get_template_directory() . '/single-produit.php' );
exit();
}
}
add_action( 'template_redirect', 'front_page_redirect' );


dameer comments:

Yeah! Here's a "merged" and thus final version of your answer:

function k_enable_front_page_cpt( $query ) {
if( !isset( $query->query_vars[ 'post_type' ] ) || '' == $query->query_vars[ 'post_type' ] && 0 != $query->query_vars[ 'page_id' ] ) $query->query_vars[ 'post_type' ] = array( 'page', 'page_blox' );
}
add_action( 'pre_get_posts', 'k_enable_front_page_cpt' );

function k_template_redirect_frontpage_cpt() {
if( is_front_page() && is_singular( 'page_blox' ) ) {
include locate_template( '/single-page_blox.php' );
die();
}
}
add_action( 'template_redirect', 'k_template_redirect_frontpage_cpt' );


Thanks a bunch for all your efforts! Hopefully there are more people in need for something like that.

2013-10-23

Balanean Corneliu answers:

Go to Setting/permalinks and chose what you whant.


Balanean Corneliu comments:

From your description you have selected Default : /?p=123
You can chose Custom Structure and set for your own needs.


dameer comments:

This doesn't have anything to do with the subject. The point is I have added my custom post type 'page_blox' to the list of 'Front page displays > Static page' drop-down which means any 'page_blox' Page can be selected for a static home page.
However, (I suppose) there's no rewrite rule for a custom post type Page when displayed on index.


Balanean Corneliu comments:

Can you show us your entire code of the custom post type?


dameer comments:

Sure...

$args_page_blox = array(
'labels' => $labels_page_blox,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'blox', 'with_front' => false ),
'capability_type' => 'page',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 20,
'supports' => array( 'title', 'author', 'thumbnail' ),
);
register_post_type( 'page_blox', $args_page_blox );


Balanean Corneliu comments:

Why you dont make a custom post type with "init"
take a look here http://codex.wordpress.org/Post_Types


dameer comments:

This doesn't make any difference or impact on how WP rewrite rules are applied on static page selected for home page.


Balanean Corneliu comments:

http://www.prodeveloper.org/create-your-own-rewrite-rules-in-wordpress.html

All about rewrite rules


dameer comments:

Please don't walk me around mate, I know how to google for stuff. I wouldn't be here and paying for the answer otherwise.


Balanean Corneliu comments:

If you pay does not mean you don't needt to respect. Navjot asked you if you saved. Often solving was a trivial thing.


dameer comments:

Respect!

2013-10-23

Hariprasad Vijayan answers:

Hello,

Change permalink to /%postname%/ from settings.


dameer comments:

Permalink structure does not have anything to with it. When any other, regular Page is selected for static home page no special permalink structure is needed.

2013-10-23

Navjot Singh answers:

Did you resave the permalinks from the settings after creating the post type?


dameer comments:

At least 792 times so far.


Navjot Singh comments:

Just because you are paying for the solution doesn't mean you can act rude. Atleast listen to what others are saying. If you don't like the solutions offered here, you are welcome to try elsewhere.

Also if you have googled and haven't come across this link: http://wordpress.stackexchange.com/a/48707/5730 then check and do try the enable_front_page_stacks function in your theme's function.php. Might work. Also I don't think I need to tell that you need to replace wpwebninar with page_blox in the function.


dameer comments:

I didn't mean to be rude at all, my answer should be kinda funny because asking me to do such a trivial thing as resaving permalinks IS FUNNY.
Next, asking me to take a look at different sites doesn't answer my question. If you can't assemble a couple of lines of code to start discussing upon then don't participate.
And yes, I have paid for an answer that's why I expect a solid answer instead of a link, and that's why I'm not "elsewhere".


Navjot Singh comments:

What's the point when similar queries to yours have been answered elsewhere? We are trying to point you out in the right direction but you want someone to write a long code here because I guess only that will led you to believe that you are getting worth for your solution.

I am sorry but that's not always possible and can't happen. Atleast give any solution posted above a shot. Everyone is trying to help you. Nobody is trying to rip you off.


dameer comments:

The point is that my question/problem hasn't been resolved yet. Otherwise I wouldn't pay for it.
I have seen http://wordpress.stackexchange.com/a/48707/5730, gave it a shot 2 days ago and - as you can see by yourself - none of the answers has been accepted. It doesn't work that way, please give it a shot before thinking it's the right one.
I've seen this one too : http://wpquestions.com/question/show/id/2944
...and a bunch of all others across the web.

So, thanks for your efforts mate, I DO APPRECIATE IT! But please provide some code instead.