Upstatement / routes

Simple routing for WordPress
188 stars 39 forks source link

README for custom WP_Query is incorrect #7

Open njbarrett opened 7 years ago

njbarrett commented 7 years ago

I defined the following route:

Routes::map('category/:category/:subcategory', function($params) {
    $category = $params['category'];
    $subcategory = $params['subcategory'];
    $query = new \WP_Query(array(
        'post_type' => 'post',
        'category_name' => $category,
        'tax_query' => array(
            array(
                'taxonomy' => 'secondary_category',
                'field'    => 'slug',
                'terms'    => $subcategory,
            ),
        ),
    ));
    Routes::load('archive.php', array('category' => $category, 'subcategory' => $subcategory), $query);
});

But I get the error:

Warning: parse_str() expects parameter 1 to be string, object given in /upstatement/routes/Routes.php on line 139

In fact you just need to pass the arguments array, not a WP_Query object:

Routes::map('category/:category/:subcategory', function($params) {
    $category = $params['category'];
    $subcategory = $params['subcategory'];
    $query = array(
        'post_type' => 'post',
        'category_name' => $category,
        'tax_query' => array(
            array(
                'taxonomy' => 'secondary_category',
                'field'    => 'slug',
                'terms'    => $subcategory,
            ),
        ),
    );
    Routes::load('archive.php', array('category' => $category, 'subcategory' => $subcategory), $query);
});
wisefool commented 7 years ago

You are a scholar and a gentleman. This was killing me.