I need to use the URL parameter ?m=1 to load every page of my wordpress with a different template but the problem is that I can't pull the text and posts that can be accessed using the canonical URL I tried different methods the best one that worked for me is using template_redirect action hook or template_include filter the problem is that I can't load data to the template file using any of this method the template load but the page is blank and with some methods it returns 404 error to better explain my problem

I added this code to the end of functions.php

add_filter( 'template_include', 'custom_template' );

function custom_template($template) {
   global $post, $wp_query;
  if ($wp_query->query_vars['m'] === '1') {
    return TEMPLATEPATH . '/custom-template.php';
  } else {
    return $template;

I can't load the posts to the template file custom-template.php with this code it returns blank

<div id="content">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
          <h2 class="item"><span class="fn"><?php the_title(); ?></span></h2>
        <?php the_content(); ?>
    <?php endwhile; else: ?>
    <?php endif; ?>

the code render blank

<div id="content">

I need to have a custom template with the URL parameter ?m=1 and load the same post in the canonical URL is there any working method to show custom template with valid post data and headers using the url parameter ?m=1

  • Try with something more unique than m, that's already a WordPress query var.
    – Milo
    Commented Dec 28, 2014 at 3:17
  • I know that m is a reserved wordpress query but I'm looking for a workaround as I need to show posts with the same query Commented Dec 28, 2014 at 9:52
  • I'm not sure I understand- why can't you change m to something else?
    – Milo
    Commented Dec 28, 2014 at 17:24

1 Answer 1


here is the full code to force change theme by a reserved wordpress parameter


function my_pre_get_posts($wp_query) {
    global $wpdb;

    // Confirm request is main query
    if(!$wp_query->is_main_query()) {

    // Get post name from query
    $post_name = $wp_query->get('pagename');

    // Get post type from database
    $post_type = $wpdb->get_var(
            'SELECT post_type FROM ' . $wpdb->posts . ' WHERE post_name = %s LIMIT 1',

    // Confirm page is post
    if ($post_type === 'post') {
        $wp_query->set('m', '');


function my_template_redirect() {
    global $post, $wp_query;

    if ($wp_query->query['m'] === '123') {
        include TEMPLATEPATH . '/template-name.php';

you can also use the template include method instead of template_redirect hook as long as you add global $wp_query to your function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.