• Hi everyone,

    I have been looking for a way to get my query vars in to permalink format for what seems to be about a week now with no luck getting it to work correctly at all.

    I have tried using add_rewrite_rule, add_rewrite_tag, using the generate permalink structure method (in $wp_rewrite) and none of them ever seem to work correctly!

    Permalinks are correctly enabled and not set to default as they are on installation.

    The scenario:
    I have a portfolio site that uses a custom post type (using the CCTM plugin for the custom fields and custom post types) to create a type for a portfolio item. The item is similar to a regular post and has a title, content body and a single image (all required to create).

    I created a single page called “projects” which is then grabbed by the theme template with “page-projects.php” in the theme I created, every thing works fine.

    This page uses pagination and will also filter by the category attached the portfolio item, I will be adding a small form to filter by category which will add the query argument to the url as well as the current page (if needed and visa versa).

    Eg. https://www.example.com/projects/page/1/category/drawings
    would tell wordpress to use this URL via permalink rewrite:
    https://www.example.com?name=projects&pageno=1&category=drawings

    Every thing works correctly more or less except as you can guess the permalink, the second url would work correctly as well as the standard default permalinks enabled through settings( https://www.example.com/projects/?pageno=1&category=drawings)

    From tweaking the rewrite rules in the past the url either changed its self or showed a 404 error or redirected me to a different page completely such a post with similar content to the arguments.

    Here are my current contents for the theme’s function files:

    <?php
    
    add_theme_support("post-thumbnails");
    
    function pjr_qvars($vars)
    {
        $vars[] = "category";
        $vars[] = "pageno";
        return $vars;
    }
    add_filter("query_vars", "pjr_qvars");
    
    function pjr_rewrite()
    {
        add_rewrite_rule('projects/pageno/([^/]+)/?', 'index.php?name=projects&pageno=1', 'top');
        flush_rewrite_rules();
    }
    add_action("init", "pjr_rewrite");
    
    ?>

    Also, the contents of page-projects.php

    <?php get_header(); ?>
    
    <h2 class="grid_12">
        Latest <b>Projects</b>
    </h2>
    
    <?php
    add_rewrite_tag("%category%", "(.+?)", "category=");
    $query_args = array
        (
        'post_type' => 'portfolio_item',
        'posts_per_page' => 1,
        'paged' => (get_query_var("pageno")) ? get_query_var("pageno") : 1,
        'category_name' => (get_query_var("category")) ? get_query_var("category") : ""
    );
    query_posts($query_args);
    
    if (have_posts())
    {
        echo '<ul id="portfolio-list" class="grid_12">';
        while (have_posts())
        {
            the_post();
            $thumb = get_custom_field("portfolio_image:to_image_array", "thumbnail");
            ?>
            <li>
                <a href="<?php the_permalink ?>">
                    <img src="<?php echo $thumb[0]; ?>" alt="<?php the_title(); ?>" />
                </a>
            </li>
            <?php
        }
        echo '</ul>';
    
        //Pagination
        $page_args = array
        (
            'base' => add_query_arg("pageno" ,"%#%"),
            'format' => 'pageno/%#%',
            'current' => (get_query_var("paged")) ? get_query_var("paged") : 1,
            'total' => $wp_query->max_num_pages
        );
        echo paginate_links($page_args);
    } else
    {
        echo "No posts";
    }
    
    get_footer();
    ?>

    Please help me, I have been tearing my hair out for days and my stubborn nature won’t let this go!

    If any one needs more info please ask. ??

  • The topic ‘Custom public query vars with permalink rewrite not working’ is closed to new replies.