Per the wordpress codex pre_get_posts() does not work everywhere.
is_font_page()
will not work but is_home()
will. So your condition is_front_page() && is_home()
will fail every time. However ‘is_home()’ alone should work.
It might be helpful to others to know what exactly you are trying to do. Usually pre_get_posts is used to alter a query but from the snippet of code you have shared. All I can tell you are trying to do is get a post id of for a page that has a list of posts on it?
One more thing. I loaded your function within a theme I am working on and the function fired about 5 times in various places where new queries are made like the nav menu and sidebar. So you also need to be aware of what query you are in. You should probably use this to ensure you are in the main query.
if ( $wp_query->is_main_query() ) {
//check if home or else where here
}
I see you have added more info in the comments of your question which I am unable to comment in since my reputation is too low at this point.
If I were you I would target the homepage a different way. Maybe a custom template file and then set your query for that page with query_posts($args) and build your own loop.
Added Working Function Accepted in Chat:
function show_id ( $wp_query ) {
// lets make sure we are in the main query
if ( $wp_query->is_main_query() ) {
if ( is_home() ) {
// Default homepage or blog Archive
$id = $id = get_option( 'page_for_posts' );;
} elseif ( !is_single() && !empty($wp_query->query_vars['page_id'])) {
// static homepage aka front page
$id = $wp_query->query_vars['page_id'];
} else {
//everything else
$id = get_queried_object_id();
}
}
// Debug
echo '<pre>';
var_dump($id);
//var_dump($wp_query->query_vars['page_id']);
//var_dump($wp_query);
echo '</pre>';
}
add_action ( 'pre_get_posts', 'show_id' );