Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

  1. 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.

  2. 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');       
    }
    
  3. Added the type variable to query_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;
    }
    
  4. 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.

share|improve this question
1  
or get_query_var('type') === 'pro' ... –  onetrickpony Jul 21 '11 at 7:57
add comment

2 Answers

up vote 1 down vote accepted

look for the string page/ and capture just the part of the url before it with strpos and substr:

$url = '/players/type/pros/page/3/';
$find = 'page/';
$trimmed_url = substr( $url, 0, strpos($url, $find) );

EDIT - so in the context of your question you could use:

$url = $_SERVER['REQUEST_URI'];
$find   = 'page/';
$pos = strpos($url, $find);
if($pos):
    $url = substr($url, 0, $pos);
endif;

if( $url == '/players/type/pros/' ){
    //run the query to list the pros
}

But a question I have - why are you using the URL to determine what to load, can you not check $_GET['type'] or whatever query vars are being set?

share|improve this answer
    
Thanks Milo, I tried to accept both answers here as I believe they both would have worked with my problem. Over the last 4-5 hours I've been looking into wp rewrite, query vars and completely rethought the way I was handling it all. It seems better to do it with wordpress' rewrite system and up until today I had no idea about it. I'll update my answer soon with the method I went. I did try it with $_GET first but it didn't work because the url didn't have ?type= in them. Maybe I was doing it wrong but I've learnt so much today! –  Andrew Jul 17 '11 at 6:12
add comment

You could explode the string out into individual pieces (as separated by the "/" character) and then analyze the pieces.

<?php $url_pieces = explode('/', $_SERVER['REQUEST_URI']); ?>

In your first example, $url_pieces[0] would be "players", $url_pieces[1] would be "type", and so on.

If you wanted to make sure "players" was in the first slot and "pros" was in the third, you do something like:

<?php if (($url_pieces[0] == 'players') && ($url_pieces[2] == 'pros')) { // do stuff ?>

If all you cared about was "pros" just being in there somewhere, you could evaluate more simply:

<?php if (in_array('pros', $url_pieces)) { // do stuff ?>
share|improve this answer
    
Thanks Samuel for your answer. I've updated my question above to include the method I went with. –  Andrew Jul 17 '11 at 6:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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