Hi
I've got a nasty problem that i can't seem to get rid of.
In an attempt to remove slugs for a custom post type I've followed this guide:
We got this working but with updates it somehow broke. Now the WP_Query modifications result in a 404 error page, while the post exists!
So this is my function, with debug code:
function custom_pre_get_posts($query) {
global $wpdb;
if(!$query->is_main_query()) {
return;
}
//echo '$query before: <pre>' . print_r($query, true) . '</pre>';
$post_name = $query->get('category_name');
//$post_name = $query->get('name');
$post_name = str_replace(".html", "", $post_name);
$post_id = $wpdb->get_var( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->posts . ' WHERE post_name = %s LIMIT 1', $post_name ) );
//echo '$post_name: <pre>' . $post_name . '</pre>';
$post_type = $wpdb->get_var(
$wpdb->prepare(
'SELECT post_type FROM ' . $wpdb->posts . ' WHERE post_name = %s LIMIT 1',
$post_name
)
);
// echo '$post_type: <pre>' . $post_type . '</pre>';
switch($post_type) {
case 'product':
$query->set('product', $post_name);
//$query->set('name', $post_name);
//$query->set('p', $post_id);
//$query->set('page_id', $post_id);
$query->set('post_type', $post_type);
$query->is_single = true;
$query->is_page = false;
break;
}
echo '$query after: <pre>' . print_r($query, true) . '</pre>';
return $query;
}
which results in a 404 with this query after:
WP_Query Object
(
[query_vars] => Array
(
[category_name] => m-double-you-sachet-whey-isolate.html
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 21037
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[tag] =>
[cat] =>
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[comments_popup] =>
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[fields] =>
[menu_order] =>
...
[product] => m-double-you-sachet-whey-isolate
[post_type] => product
)
...
[is_admin] =>
[is_attachment] =>
[is_singular] =>
[is_robots] =>
[is_posts_page] =>
[is_post_type_archive] =>
[query_vars_hash] => 60f1fdf5aebc76aba270391b0658101c
[query_vars_changed] =>
[thumbnails_cached] =>
[stopwords:WP_Query:private] =>
[query] => Array
(
[category_name] => m-double-you-sachet-whey-isolate.html
)
)
I've removed some for brevity. Trying to load a post id with a valid ID does not work as well.
How do i go from here?
How do i debug it?
The post is valid and under the id as found by the function
Thanks!