I have an IF statement shown below that runs one of 2 custom queries, based on the url. The problem I'm facing is if the url contains pagination such as /page/2/
, my query breaks because the url isn't correctly matched. I'm very new to php and in my searching I've come across a few different php functions that may help my situation - I'm just unsure of how to use them and which is more efficient for my needs. These are preg_match
, parse_url
, and dirname
.
For example, site.com/players
lists the players and site.com/players/type/pro
lists the pro players. The latter has been rewritten and is actually site.com/players/?type=pro
.
The following if statement is what I have now and will not run the first query if the url has anything after the /pros/ segment, such as pagination in my case:
if ( $_SERVER['REQUEST_URI'] == '/players/type/pros/' ) {
//run the query to list the pros
}
else {
//run the default query for the players
}
Here's what I need to do, which works, but without all the manual labour of writing an OR for every single page. This is where I'm guessing one of those php functions would come into play.
if ( $_SERVER['REQUEST_URI'] == '/players/type/pros/' || $_SERVER['REQUEST_URI'] == '/players/type/pros/page/2/' || $_SERVER['REQUEST_URI'] == '/players/type/pros/page/3/' || $_SERVER['REQUEST_URI'] == '/players/type/pros/page/4/' ) {
//run the query to list the pros
}
else {
//run the default query for the players
}
Update
Thanks to those who answered. I've been digging into WordPress' rewriting and with the help of some very informative posts on this site I ended up doing it this way.
Made a new page template called player-types.php which would handle the incoming query or queries. I may need to further list players by other types.
Setup some rewrite rules placed in functions.php
add_filter('init', 'add_page_rewrite_rules'); function add_page_rewrite_rules() { //would match www.site.com/players/type/pro add_rewrite_rule('^players/type/([^/]*)/?$','index.php?pagename=player-types&type=$matches[1]','top'); //would match www.site.com/players/type/pro/page/2 add_rewrite_rule('^players/type/([^/]*)/page/([0-9]+)?$','index.php?pagename=player-types&type=$matches[1]&paged=$matches[2]','top'); }
Added the
type
variable toquery_vars
so I could use it on the new page template (placed in functions.php)add_filter( 'query_vars', 'add_query_vars' ); function add_query_vars( $query_vars ) { $query_vars[] = 'type'; //add type to query vars return $query_vars; }
And the page template in it's most basic form looks like this
<?php /* Template Name: Player Types */ ?> <?php if ( $wp_query->query_vars['type'] == 'pro' ) { //custom pro query } ?>
This allows me to have www.site.com/players/type/pro
and I can expand on it to www.site.com/players/type/whatever-else
. Normal players are still listed at www.site.com/players/
.
And for those who are wondering, I didn't do it with a taxonomy because I'm listing the players based on their custom WordPress role ( pulling the author id's into the query) rather than having a check box that someone has to manually check.
get_query_var('type') === 'pro'
...