I have a custom post type I've written and I've managed to add them to display on the "Reading Settings" in the dropdown like normal pages. That works.
Here's my problem...
When I select the specific custom post type page I want displayed as the "Static Front Page", then I go to my homepage of the site, it automatically redirects to the custom post type page.
So if my site is http://www.example.com
My page redirects to http://www.example.com/post-type-slug/post-name
I want it display only http://www.example.com just like if I selected a typical "page".
Here's the code I'm using currently...
add_filter( 'get_pages', 'add_my_salesletters' );
function add_my_salesletters( $pages ) {
$my_salesletters_pages = new WP_Query( array( 'post_type' => 'salesletters' ) );
if ( $my_salesletters_pages->post_count > 0 )
{
$pages = array_merge( $pages, $my_salesletters_pages->posts );
}
return $pages;
}
"salesletters" is my custom post type name.
IMPORTANT: I don't want ALL the post type posts to be displayed. I want a SINGLE CUSTOM POST TYPE POST. That's why I added the custom post type pages to the Reading Setting dropdown, in order to select a single post type page to be displayed as the Front page.
I don't want a list of all the Customer Post Type Posts made.
I just want a single post type page I select.
I hope that helps.
<strong>UPDATE:</strong>
Upon looking at this closely, I think it's a rewrite rule that needs to be added.
I tried an alternative...
function enable_front_page_stacks( $query ){
if('' == $query->query_vars['post_type'] && 0 != $query->query_vars['page_id'])
$query->query_vars['post_type'] = array( 'page', 'salesletters' );
}
add_action( 'pre_get_posts', 'enable_front_page_stacks' );
It stops the redirect, but now the page is stripped of the Custom Post Template so no content is shown.
Julian Lannigan answers:
You could try this:
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ( is_home() && false == $query->query_vars['suppress_filters'] )
$query->set( 'post_type', array( 'salesletters', 'attachment' ) );
return $query;
}
add that to your functions.php file.
Here is the source: [[LINK href="http://justintadlock.com/archives/2010/02/02/showing-custom-post-types-on-your-home-blog-page"]]Showing custom post types on your home/blog page[[/LINK]]
Armand Morin comments:
I don't want ALL the post type pages displayed. I want a SINGLE POST TYPE PAGE. That's why I added the custom post type pages to the Reading setting dropdown, in order to select a single post type page to be displayed as the Front page.
I don't want a list of all the Customer Post Type Posts made.
I just want a single post type page I select.
Julian Lannigan comments:
I see, try this:
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ( is_home() && false == $query->query_vars['suppress_filters'] )
$query->set( 'p' => get_option("page_on_front") );
return $query;
}
Armand Morin comments:
Got this when I ran it...
Parse error: syntax error, unexpected T_DOUBLE_ARROW
Julian Lannigan comments:
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ( is_home() && false == $query->query_vars['suppress_filters'] )
$query->set( 'p' => intval(get_option("page_on_front")) );
return $query;
}
Julian Lannigan comments:
Sorry bad formatting on the last one.
Try this:
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ( is_home() && false == $query->query_vars['suppress_filters'] )
$query->set( 'p' => intval(get_option("page_on_front")) );
return $query;
}
It basically just forces the value in the "page_on_front" option to be a integer.
Armand Morin comments:
Julienne,
Sorry, I'm getting the same error message.
It's this line which is causing it... I can't figure it out...
$query->set( 'p' => intval(get_option("page_on_front")) );
Julian Lannigan comments:
What is the output of the get_option?
Insert this into your functions.php and tell me what the result is:
var_dump(get_option("page_on_front")); die;
Armand Morin comments:
The result is 1483 which is the ID of the post I want to use.
Julian Lannigan comments:
I am sooo sorry, didn't even see this. The arguments must be encapsulated in an array, my bad.
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ( is_home() && false == $query->query_vars['suppress_filters'] )
$query->set( array('p' => get_option("page_on_front")) );
return $query;
}
Julian Lannigan comments:
Im sorry, I'm all over the place, try this if the above doesn't work:
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ( is_home() && false == $query->query_vars['suppress_filters'] )
$query->set( 'p', get_option("page_on_front") );
return $query;
}
Armand Morin comments:
I tried both and I still get the redirect the the post URL. Damn, I really hoped it would work.
Armand Morin comments:
I am wondering if it's some setting on the custom post type, that I did not correctly include or setup.
Armand Morin comments:
Upon looking at this closely, I think it's a rewrite rule that needs to be added.
I tried an alternative...
function enable_front_page_stacks( $query ){
if('' == $query->query_vars['post_type'] && 0 != $query->query_vars['page_id'])
$query->query_vars['post_type'] = array( 'page', 'salesletters' );
}
add_action( 'pre_get_posts', 'enable_front_page_stacks' );
It stops the redirect, but now the page is stripped of the Custom Post Template so no content is shown.
Julian Lannigan comments:
Does this redirect you?
add_filter( 'pre_get_posts', 'enable_front_page_stacks' );
function enable_front_page_stacks( $query ) {
if ( $query->is_home && false == $query->query_vars['suppress_filters'] )
$query->set( array('p' => get_option("page_on_front")) );
return $query;
}
Armand Morin comments:
I tried and 1.) yes, it still redirects... 2.) I get this error message...
Warning: Missing argument 2 for WP_Query::set(), on my Custom Post Type Page
Julian Lannigan comments:
I don't know what I keep thinking...I have to pass it as 2 arguments and not an array. Please try this.
add_filter( 'pre_get_posts', 'enable_front_page_stacks' );
function enable_front_page_stacks( $query ) {
if ( $query->is_home && false == $query->query_vars['suppress_filters'] )
$query->set( 'p', get_option("page_on_front") );
return $query;
}
If that doesn't work could you run the following so that I can see the query we are initially working with.
add_filter( 'pre_get_posts', 'enable_front_page_stacks' );
function enable_front_page_stacks( $query ) {
if ( $query->is_home && false == $query->query_vars['suppress_filters'] ) {
echo "<pre>".print_r($query,1)."</pre>";die;
}
return $query;
}
Please paste in the results of the above code if the first block does no changes. (please paste it into a code block so it preserves the indentation)
Armand Morin comments:
Ok the first code didn't work and STILL redirected...
Here's the code produced by the second code...
WP_Query Object
(
[query_vars] => Array
(
[post_type] => salesletters
[error] =>
[m] => 0
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[comments_popup] =>
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[fields] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
)
[tax_query] => WP_Tax_Query Object
(
[queries] => Array
(
)
[relation] => AND
)
[meta_query] =>
[post_count] => 0
[current_post] => -1
[in_the_loop] =>
[comment_count] => 0
[current_comment] => -1
[found_posts] => 0
[max_num_pages] => 0
[max_num_comment_pages] => 0
[is_single] =>
[is_preview] =>
[is_page] =>
[is_archive] =>
[is_date] =>
[is_year] =>
[is_month] =>
[is_day] =>
[is_time] =>
[is_author] =>
[is_category] =>
[is_tag] =>
[is_tax] =>
[is_search] =>
[is_feed] =>
[is_comment_feed] =>
[is_trackback] =>
[is_home] => 1
[is_404] =>
[is_comments_popup] =>
[is_paged] =>
[is_admin] =>
[is_attachment] =>
[is_singular] =>
[is_robots] =>
[is_posts_page] =>
[is_post_type_archive] =>
[query_vars_hash] => f34f628bb13defc6470b8d5e629a3d1f
[query_vars_changed] =>
[thumbnails_cached] =>
[query] => Array
(
[post_type] => salesletters
)
)
Julian Lannigan comments:
Try this out, I'm trying a different action.
add_filter( 'parse_query', 'enable_front_page_stacks' );
function enable_front_page_stacks( $query ) {
if ( $query->is_home && false == $query->query_vars['suppress_filters'] ) {
$query->query_vars['p'] = get_option("page_on_front");
}
return $query;
}
Armand Morin comments:
I'm sad say... no it still redirected.
Is there anything I can do to help you on this?
Julian Lannigan comments:
I don't necessarily like the way I did this, but it works.
I made the changes in your CORE.php but here is the code that I changed:
add_filter( 'template_redirect', 'enable_front_page_stacks', 1);
function enable_front_page_stacks($template) {
if ( $_SERVER["REQUEST_URI"] == "/" ) {
query_posts("p=".get_option("page_on_front")."&post_type=salesletters");
the_post();
include(plugin_dir_path(__FILE__) . "/single-salesletter.php");
die;
}
return $template;
}
Armand Morin comments:
Awesome... i know what you mean. But it works. I think at this time, that's the only way it will work. Thank you... you no doubtedly won this.
Ryan Riatno answers:
You should create a custom page templates and run query from theme then set this page as front page.
Armand Morin comments:
I don't want ALL the post type pages displayed. I want a SINGLE POST TYPE PAGE. That's why I added the custom post type pages to the Reading setting dropdown, in order to select a single post type page to be displayed as the Front page.
I don't want a list of all the Customer Post Type Posts made.
I just want a single post type page I select.
Utkarsh Kukreti answers:
Add this to your functions.php
add_action("template_redirect", "cpt_on_home");
function cpt_on_home() {
if(is_home())
query_posts(array( 'post_type' => 'salesletters' ));
}
Armand Morin comments:
I don't want ALL the post type pages displayed. I want a SINGLE POST TYPE PAGE. That's why I added the custom post type pages to the Reading setting dropdown, in order to select a single post type page to be displayed as the Front page.
I don't want a list of all the Customer Post Type Posts made.
I just want a single post type page I select.
Utkarsh Kukreti comments:
add_action("template_redirect", "cpt_on_home");
function cpt_on_home() {
if(is_home())
query_posts(array( 'p' => get_option("page_on_front") ));
}
Armand Morin comments:
Utkarsh,
I tried this and it still redirects as in my description above.
Utkarsh Kukreti comments:
Could I have wp-admin access to the site to confirm?
Utkarsh Kukreti comments:
You can contact me from my profile.
Utkarsh Kukreti comments:
Does this redirect?
add_action("wp_head", "cpt_on_home");
function cpt_on_home() {
if(is_home())
query_posts(array( 'p' => get_option("page_on_front") ));
}
Armand Morin comments:
Yes, it redirects. Maybe it's something with the way I'm bringing in the CPT into the dropdown. Possibly it's causing something there.
Utkarsh Kukreti comments:
Could you link to the site?
Armand Morin comments:
I'm running on xampp on my own computer. Can't link to site. I'll try to post to site in a few.
Armand Morin comments:
Here's a test site I've setup.
http://internetmarketingz.com
You can see the page redirects no matter what.
Peter Michael answers:
Leave your reading setting on the standard values. Create a file 'home.php' in your theme folder and create a loop which pulls your custom post type, i.e.
<?php
# File home.php
get_header(); ?>
<div id="container">
<div id="content" role="main">
<?php query_posts( array( 'post_type' => 'salesletters' ) ); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2 class="entry-title"><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_excerpt(); ?>
</div><!-- .entry-content -->
</div><!-- #post-## -->
<?php endwhile; // end of the loop. ?>
<?php wp_reset_query(); ?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Armand Morin comments:
I don't want ALL the post type pages displayed. I want a SINGLE POST TYPE PAGE. That's why I added the custom post type pages to the Reading setting dropdown, in order to select a single post type page to be displayed as the Front page.
I don't want a list of all the Customer Post Type Posts made.
See my point?
Peter Michael comments:
See your point, and you can achieve that by changing
query_posts( array( 'post_type' => 'salesletters' ) ); ?>
See [[LINK href="http://codex.wordpress.org/Function_Reference/query_posts"]]http://codex.wordpress.org/Function_Reference/query_posts[[/LINK]]
Grégory Viguier answers:
Hi.
Try this one, it works for me:
Create a front-page.php file (not home.php) (you can copy your single.php file, rename it and modify it with this query) and put this query inside :
<?php
// Beginning of your template with get_header()...
// Now the new query
$temp = $wp_query;
$wp_query= null;
$args = array(
'p' => YOUR_POST_TYPE_ID,
'post_type' => 'salesletters',
'post_status' => 'publish'
);
$wp_query = new WP_Query( $args );
// Beginning the loop
while ($wp_query->have_posts()) : $wp_query->the_post();
// Do your stuff with the_content(), the_title()...
endwhile; // End of the loop
// Retrieve the initial query
$wp_query = null; $wp_query = $temp;
// Now the end of your template with get_footer()...
?>
Armand Morin comments:
Gregory,
You're right, you CAN do it like this.
But here's my situation. I'm doing this inside a plugin I'm building, so I need it to work by putting the Custom Post Types in the dropdown on the Reading Settings and then make the homepage work like normal.
If it wasn't for a plugin, your option would work.
AMYunus WP answers:
Hi Armand, have you tried to use update_option( 'show_on_front' ) with the integer id of the page? :)
update: because I think if you want to make dropdown of the reading setting, you must edit wp-admin/options-reading.php. then add a value array 'post_type' => 'page' of wp_dropdown_pages.
I think this way will set your front page without configure it on reading setting panel
update again: oh what a shame, I answer the same as before me. I'm sorry.