[wp-hackers] Custom Post Types, Pretty URLs and Custom Query Vars

ErisDS erisds at gmail.com
Tue Nov 16 17:30:08 UTC 2010


I am currently in the process of adding a custom post type (job) to a
WordPress install via a plugin.

I have successfully setup the custom post type, added some meta boxes,
gotten everything saving happily, stopped autosave from removing my custom
fields etc etc.

I have also installed the Custom Post Type Archives plugin so that I have
nice URLs
http://mywp.com/jobs/ works as a list of jobs
http://mywp.com/jobs/job-title goes to individual job posts

jobs have a custom field related to them of "company"

However, I also want to have the URL structure...

http://mywp.com/jobs/company/3

...show a list of jobs which are all with the company of id 3 (it'd be nice
to use the company name, but for now id is fine)
Pagination must still work so

http://mywp.com/jobs/company/3/page/2 needs to also work

First of all, I have registered my query var:

function setup_query_vars( $query_vars )
  {
    $query_vars[] = 'company';
    return $query_vars;
  }
add_filter('query_vars', 'setup_query_vars');


Also, I have setup a filter so that if the post type is job, and the company
query var is set, wp_query is updated like so:
function filter_for_company_jobs($wp_query)
  {
    if(!is_admin() && isset($wp_query->query_vars['post_type']) &&
$wp_query->query_vars['post_type'] == 'job' &&
isset($wp_query->query_vars['company']))
    {
      $wp_query->query_vars['meta_key'] = 'company';
      $wp_query->query_vars['meta_compare'] = '=';
      $wp_query->query_vars['meta_value'] =
$wp_query->query_vars['company'];
    }
  }
 add_action( 'parse_query', 'filter_for_company_jobs', 90 );

So far so good, the URL:

http://mywp.com/jobs/?company=3

works and so does

http://mywp.com/jobs/page/2/?company=3

So all I need to do is get a rewrite working so that the URL structure is
"pretty".
However every attempt to do this results in my "company" query var
disappearing. Pagination remains, but I end up with a full list of jobs, not
just those relating to the company, and there is no record of "company" in
$wp_query.

This is how I'm doing my rewrite:

function setup_rewrite_rules($wp_rewrite)
  {
   $custom_rules = array();

   $custom_rules["job-vacancies/company/?([0-9]{1,})/?$"] = // enable
company
   "index.php?post_type_index=1&post_type=job&company=" .
$wp_rewrite->preg_index(2);

   $wp_rewrite->rules = array_merge($custom_rules, $wp_rewrite->rules); //
merge existing rules with custom ones

   return $wp_rewrite;
  }
add_filter('generate_rewrite_rules',  'setup_rewrite_rules');

I originally thought something might be going wrong with the archive plugin,
however, I've tried all the ones I could find and they all result in the
same problem,
Plus, if I reset the post type to post so it uses the index.php template I
still lose track of the query var, so I think it must be me/the rewrite
rule!

Any ideas what I'm doing wrong?

Sorry this is long, just trying to be thorough!

Eris


More information about the wp-hackers mailing list