WordPress.org

Support

Support » How-To and Troubleshooting » Need to GET the slug value for use in another data table…

Need to GET the slug value for use in another data table…

  • In an earlier problem, which is related this one, I need to get the ID of a Tag. t31os_ gave me this for the tag_ID:

    <?php echo get_query_var('tag_id'); ?>

    That’s not working for the slug. I’m trying:

    <?php echo get_query_var('slug'); ?>

    I want to get the Slug so I can then match it with a column in another table within my WP database. Any help?

Viewing 15 replies - 1 through 15 (of 29 total)
  • BTW…these are Slugs for Tags, not Categories, Posts, Pages, etc.

    I”m using to echo my results:

    mysql_select_db("jwrbloom_wpHHR");
    
     $wp_tagID = get_query_var('tag_id');
     $wp_slug = get_query_var('single_tag_title');
    
     echo $wp_tagID;
     echo $wp_slug;

    $wp_tagID is producing the correct results.
    $wp_slug isn’t producing anything.

    [moderated–bump removed. Please refrain from bumping as per Forum Rules]

    MichaelH

    @michaelh

    In The Loop could be just this:

    echo $post->post_name;
    get_query_var('tag')

    – queried tag name

    The only ones that handle tag slugs are ..

    get_query_var('tag_slug__in')

    and

    get_query_var('tag_slug__and')

    Though i’m not sure whether these values are populated in all tag queries.

    I think the alternative is to just take what you need from this..

    $wp_tagID = get_query_var( 'tag_id' );
    $wp_tag = get_tag( $wp_tagID );

    $wp_tag will then be an object with the following values… (example)

    [term_id] => 60
        [name] => bravo
        [slug] => bravo
        [term_group] => 0
        [term_taxonomy_id] => 62
        [taxonomy] => post_tag
        [description] =>
        [parent] => 0
        [count] => 0

    Of course the values will be different for each tag and not necessarily match my tag above.

    To reference that information..

    $wp_tag->method

    Where “method” is one of the values above, so term_id, name, slug, etc…

    MichaelH

    @michaelh

    BTW…these are Slugs for Tags, not Categories, Posts, Pages, etc.

    I missed that 😉

    Ok…as t31os_ might remember, nothing I ask is just that simple. I “get” something so I can use it, and only being able to read PHP and understanding the logic, I’m not always good at writing my own code.

    Most of my Tags are basketball player names. As Posts get published with Tags, I’m trying to match data in two tables with the “Slug” then attach the Tag’s ID to the item in my custom table.

    So I have this custom table within my WP database. As I type in the names (they are ranked by the position they play, as well as what grade they are in), I actually type in what their slug would be in WordPress. I then try to match the slugs in each table, so I can figure out which Tag ID to Update my custom table with.

    At least that’s how I understand it working. Here is the code I’m trying to use, but of course, it’s not working:

    mysql_select_db("jwrbloom_wpHHR");
    
     $wp_tagID = get_query_var('tag_id');
     $wp_slug = get_query_var('tag');
    
    $query ="UPDATE wp_playerRank-backup SET wpID = '$wp_tagID' WHERE wpSlug = '$wp_slug'";

    Part of the issue is that I may not know where to put that code. Right now, I have in the post.php file just before the script at the end of the file.

    You could of continued this in the other thread, no matter though..

    My previous reply gave you an example of grabbing tag data.

    As i pointed out in your other thread though, add die messages onto your querys and database selections so you can see when they fail, and what they fail on… Once the code works you can remove them if you must…

    $somequery = mysql_query("SELECT etc...");

    becomes…

    $somequery = mysql_query("SELECT etc...") or die( mysql_error() );

    Yes the page stops outputting data if you hit a die, but you will have a clear and precise clue as to what has failed in your query…

    As i also pointed out previously, you should check a variable has a value before using it, this is basically a one line isset check and set..

    $var = ($someothervar) ? $someothervar : 'foobar';

    … and the long way being…

    if(isset($someothervar)) { $var = $someothervar; }
    else { $var = 'foobar'; }

    ———-

    Ok back to the tags…why not just use..

    $wp_tag = &get_tag( get_query_var('tag_id') );

    You then have all the data on that tag available..

    $wp_tag->term_id // the tag id
    $wp_tag->name // tag name
    $wp_tag->slug // tag slug

    and so on… (i’ve listed the values in my last reply)..

    This is a new development or a new method of wanting to do it. As I will have multiple authors, I wanted to make this step automatic instead of manually going in and applying Tag ID’s.

    I’m sure I have all the data I need.

    Did I miss a reply? The last one you helped me on, I haven’t been back since you pointed me in the right direction. Now, I think I just need to Update my custom table, which has many more names than my WP table, as I increase the number of Tagged names.

    I’m not following what you have just typed above very well. I know I have the Tag ID, and I know I have the slug title. For example, I know the Slug alexander-hutson has the Tag ID of 68. My custom table already had the Slug value in place. I need to find the matching Slug value and Update that row of data with the 68.

    I’ve just given you a way to get the slug and name by using just the ID..

    Take this piece of code you previously posted.

    mysql_select_db("jwrbloom_wpHHR");
    
     $wp_tagID = get_query_var('tag_id');
     $wp_slug = get_query_var('tag');
    
    $query ="UPDATE wp_playerRank-backup SET wpID = '$wp_tagID' WHERE wpSlug = '$wp_slug'";

    Used with what i posted it would now look something like..

    mysql_select_db("jwrbloom_wpHHR");
    
    $wp_tag = &get_tag( get_query_var('tag_id') );
    
    $query ="UPDATE wp_playerRank-backup SET wpID = '$wp_tag->term_id' WHERE wpSlug = '$wp_tag->slug'";

    Does that make any more sense now? …

    One question should be, where do I put it? Right now it’s toward the end of…

    wp-admin/includes/post.php

    It didn’t work.

    You shouldn’t be touching any of the core WordPress files..

    If you need to update a database table then do so using your own custom written plugin..

    If you want updates to occur at particular times (like when a post is published), you should look at the hooks and filters..

    Consult here.
    http://codex.wordpress.org/Plugin_API
    for info on hooks, filters and creating plugins.

    I assumed this was custom, had no idea you were fiddling in a core file. I can understand the urge to dive in, but you’ll be better off spending times reading through information on creating a plugin and hooking onto the WordPress actions.

    I’m not making heads or tails of what to do with it. FWIW, I am putting the code in a separate PHP file and just inserting it, or at least that was my intention. To me that seems no different than creating a function then calling for that function to be executed.

    I’m just trying to Get two pieces of Tag data and Updating my custom table. I have no clue where to put it, even if I were to understand the API information you linked enough.

    Using logic based on what I read, when I publish a Post, it triggers things to happen, including Inserting Tag information. I want it to also Update a column in my custom table where I already have hundreds of a rows of player data.

    So are you saying that if I “add action” to publish_post, anything within that function is executed when I publish a post?

    Yes you can hook onto the both the save_post and publish_post actions (i’ve not done it myself, but several plugins do it).

    Is this the only query you need to run when a post is published?

    $query ="UPDATE wp_playerRank-backup SET wpID = '$wp_tagID' WHERE wpSlug = '$wp_slug'";

    You can create a connection to another database if it’s on the same account (same site/server)..

    $newdbcon = new wpdb();

    See flaminglogos thread here on creating a second database connection..
    http://wordpress.org/support/topic/307945?replies=6

Viewing 15 replies - 1 through 15 (of 29 total)
  • The topic ‘Need to GET the slug value for use in another data table…’ is closed to new replies.