Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions src/Router/RewriteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,29 +92,32 @@ public function parse_markdown_url(\WP $wp): void {
return;
}

$slug = $matches[1];

// Find post by slug - handles both posts and pages
global $wpdb;
$post_row = $wpdb->get_row($wpdb->prepare(
"SELECT ID, post_type FROM {$wpdb->posts}
WHERE post_name = %s
AND post_status = 'publish'
AND post_type IN (" . implode(',', array_fill(0, count($this->get_supported_post_types()), '%s')) . ")
LIMIT 1",
array_merge([$slug], $this->get_supported_post_types())
));

if (!$post_row) {
// Strip .md to get the original URL path, let WordPress resolve it
$clean_url = home_url('/' . $matches[1]);
$post_id = url_to_postid($clean_url);

if (!$post_id) {
return; // Let WordPress show its normal 404
}

// Get full post object and cache it for handle_markdown_request
$this->markdown_post = get_post($post_row->ID);
$post = get_post($post_id);

if (!$post || $post->post_status !== 'publish') {
return;
}

if (!$this->is_supported_post_type($post->post_type)) {
return;
}

// Cache post for handle_markdown_request
$this->markdown_post = $post;

// Set query vars for WordPress
$wp->query_vars['p'] = $post_row->ID;
$wp->query_vars['p'] = $post->ID;
$wp->query_vars['markdown_request'] = '1';
// Remove incorrect pagename that the rewrite rule may have set
unset($wp->query_vars['pagename']);
}

/**
Expand Down