• First of all, please don’t get on me for not using WordPress syntax to query a database. In my defense, I’m using a non-WP table. ??

    I’m wanting to take the tag or search slug passed via a URL to query a database to create a list of players. Those slugs will either be a school name or a player’s name, but no matter which, I need the school’s name to produce my list.

    If it’s a school’s name, it’s easy. If it’s a player’s name how do I get the school name for query?

    Do I use a sub/nested query?

    Such as

    SELECT school
    FROM
    (SELECT nameFirst,nameLast,feet,inches,city,school,grade FROM a_players
    WHERE school = ‘”.$wp_searchSlug.”‘
    AND grade>=’20′”;)

    Something like that?

    Below is what I’m using now. Obviously, if it’s player name, it produces a blank result.

    
    		 $tag = single_tag_title("", false); // the false is a display setting for WP template tags
    		 $wp_searchSlug = get_search_query();
    
     
    		if ($wp_searchSlug != "") {
    			$query = "SELECT nameFirst,nameLast,feet,inches,city,school,grade FROM a_players
    			WHERE school = '".$wp_searchSlug."'
    			AND grade>='20'";
    			
    
    		}
    		else {
    		$query = "SELECT nameFirst,nameLast,feet,inches,city,school,grade FROM a_players
    			WHERE school = '".$tag."'
    			AND grade>='20'";
    			
    		}
    
Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter jwrbloom

    (@jwrbloom)

    Here is what it looks like when a player’s name is the tag:
    https://www.courtsideindiana.com/tag/pierce-thomas/

    No results

    Here is what it looks like when a team’s name is the tag:
    https://www.courtsideindiana.com/tag/brownsburg/

    It’s able to create the header and find four rows.

    It’s the right side where I’m producing the list.

    Thread Starter jwrbloom

    (@jwrbloom)

    This is getting me closer. At least now it’s not producing any blank results. However, when it’s a player’s name, it just produces that player, not the others. (I know it wouldn’t. I just fixed the blank result issue.

    Now I need the list to produce regardless of what type of tag it is.

    
    $query = "SELECT nameFirst,nameLast,feet,inches,city,school,grade FROM a_players
    			WHERE (school = '".$tag."' || CONCAT(nameFirst,' ', nameLast) = '".$tag."')
    			AND grade>='20'";
    			
    
    Moderator bcworkz

    (@bcworkz)

    If you are not using WP functions to query a non-WP table, then it’s not really a WP question at all, is it? ??

    In any case, it’s hard to answer your questions without some example of how the tables are structured and related. I think your main issue is code does not know if the supplied tag slug is for a player or school. It’d be a lot better if they were separate taxonomies or at least they resided hierarchically below one or the other overall term.

    Of course, tags are not hierarchical, but a different taxonomy could be. Short of restructuring your tags, I suppose code could assume a school is passed and make a query accordingly. If the query fails to return anything, then it must be a player. Get that player’s school, then make the usual school query. Or the other way around. Do which ever is more efficient to query for first.

    Thread Starter jwrbloom

    (@jwrbloom)

    It’s just one table with the columns listed above.

    I’m using a plugin called Simple Tags, which links the tags inline in each Post. In the past (years ago), there was a plugin (I’m sure there still is) that lets the user set up custom taxonomies.

    I’m not so far in I can’t change that, but I do like having the tags linked in each Post.

    I do think, however, my core issue still is when it’s the Player tag, how to get it to use the Team to get the list.

    Crudely put:
    Get Tag
    Tag matches Player name
    Player plays for Team A
    Get list of Players who play for Team A

    Thread Starter jwrbloom

    (@jwrbloom)

    Tried a custom taxonomy plugin (just one so far), but the issue becomes altering how Terms are stored. Stored in the custom taxonomy, it’s not recognized as a “tag”, which impacts things like related articles and doesn’t inherently show up at the bottom of articles.

    There might be on out to help my needs, but I don’t want it impacting the core features of WordPress.

    So I’m still on track to refine my query.

    Moderator bcworkz

    (@bcworkz)

    Yeah, when you change one thing you find a bunch of related things that would need to be altered as well. It can snowball. There’s almost always a way to accomplish something, but is it worth the effort?

    The crude list in your earlier reply is fine, except there needs to be a branch for when the tag is already a school/team name and the player query fails. Naturally you still do the query for players on the team. The difference is the source of the query data: the initial tag value or the school/team associated with the initial tag value.
    Get tag
    Query for school where tag = player
    If query result is null, school is tag; else school is query value.
    Query for players where school from above = school column

    Thread Starter jwrbloom

    (@jwrbloom)

    I’m sure I can have two queries, but I’ve always been told that’s never a good idea. I’m looking for help for what seems to be a complex query.

    I need a query that when it’s a player, it does what it’s supposed to do for that player, but also list the other players at his school.

    Moderator bcworkz

    (@bcworkz)

    It’s important to minimize queries, but multiples are not forbidden where necessary. It’s also good to avoid overly complex queries. It’s conceivable two simple queries are better than one complex one. The thing is conditional logic is required in order to know what value to use to get all players. Doing that in SQL is beyond my SQL abilities, but if you can manage it then go for it.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Using tags via URL to trigger queries…’ is closed to new replies.