Need to GET the slug value for use in another data table... (30 posts)

  1. Jim R
    Posted 6 years ago #

    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?

  2. Jim R
    Posted 6 years ago #

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

  3. Jim R
    Posted 6 years ago #

    I"m using to echo my results:

     $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]

  4. MichaelH
    Posted 6 years ago #

    In The Loop could be just this:

    echo $post->post_name;
  5. Mark / t31os

    Posted 6 years ago #


    - queried tag name

    The only ones that handle tag slugs are ..




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


    Where "method" is one of the values above, so term_id, name, slug, etc...

  6. MichaelH
    Posted 6 years ago #

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

    I missed that ;)

  7. Jim R
    Posted 6 years ago #

    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:

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

  8. Mark / t31os

    Posted 6 years ago #

    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...");


    $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)..

  9. Jim R
    Posted 6 years ago #

    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.

  10. Mark / t31os

    Posted 6 years ago #

    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.

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

    $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? ...

  11. Jim R
    Posted 6 years ago #

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


  12. Jim R
    Posted 6 years ago #

    It didn't work.

  13. Mark / t31os

    Posted 6 years ago #

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

  14. Jim R
    Posted 6 years ago #

    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.

  15. Jim R
    Posted 6 years ago #

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

  16. Mark / t31os

    Posted 6 years ago #

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

  17. Jim R
    Posted 6 years ago #

    Yes, that is the only additional query, and I moved my custom table into the WP database. It was causing conflicts with the widgets. I'm assuming you think the code I have should work, at least the syntax looks good. Right?

    Am I in the correct file too, post.php?

  18. Mark / t31os

    Posted 6 years ago #

    If you have them in the same database, then even easier and dead simple to hook an action onto post saves..

    Although a little unrelated, this page has a good example of hooking onto the save status of posts/pages...

    Some information on interacting with the database.

  19. Jim R
    Posted 6 years ago #

    All that does is just confuse me right now. I don't really need to learn the kitchen sink right now, just basics for my need. The rest can come later, as I apply what I've learned.

    So I create a function...assuming it's the query above I posted. At the end of it I add:

    add_action('publish_post', 'name_of_function');

    Is that right?

    And why can't I just use a regular query in the correct location?

  20. Mark / t31os

    Posted 6 years ago #

    Can you give me a better idea of what you should happen when a post is saved in relation to your own custom table in the database..

    An exported row from that table plus an example of what a typical update does/changes would really help...

    The $wpdb class has several functions you can use to update, insert or drop from the database..

    Plugin code it's a case of..

    add_action('save_post', 'myplugin_perform');
    /* When a post or page is saved */
    function myplugin_perform( $post_id ) {
    	global $wpdb;
    	// Verify if this is an auto save routine.
    	if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
    		return $post_id;
    	// Check permissions
    	if ( 'page' == $_POST['post_type'] ) {
    		if ( !current_user_can( 'edit_page', $post_id ) )
    			return $post_id;
    	else {
    		if ( !current_user_can( 'edit_post', $post_id ) )
    			return $post_id;
    	// Do your own stuff here..

    Basic of basic examples...

    Then look at some form of prepare -> update statement..

    $wpdb->query( $wpdb->prepare("UPDATE $wpdb->tablename SET ( field1, field2 ) VALUES ( %d, %s )",$var_with_value1, $var_with_value2 WHERE x = y) );

    I've not used the prepare method before, i but think the format above is correct, only examples i could find dealt with inserts... , you can use wp->get_results("SOME QUERY") if you find that easier..

    In the above, %d tells the function this value is expected to be an integer, %s a string, then variables $var_with_value1, and $var_with_value2 are the variables holding data that go into the values, in matching order.. (works like sprintf).. field1 and field2 being the corresponding database columns/fields..

    I've been playing with some code and managed to successfully update a row in another table each time a post is saved.... it's as simple as the above basic example...

    If you only need this update to occur on the creation of new posts, and not after editting, then yes i think you might be able to hook onto the publish_post action instead..

    Play around and see what works.. (you could proberly use the code in your theme's function file for what it's worth)...

  21. Jim R
    Posted 6 years ago #

    I just want this to work:

     $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'";

    When an Author, presumably me most of the time, Posts, there will be Tags attached to that Post. For each Tag, I want it to find the matching 'tag' (Slug) in my custom table and Set the wpID column in that row with the same 'tag_id'.

    I need to know where to put that or even a function. I probably need a foreach loop now that I think about it. I didn't when I applied the Update query directly in my table to retrofit the wpSlug data retrofitting Slugs to my previously created 449 rows of data.

  22. Mark / t31os

    Posted 6 years ago #

    I don't think you can use the regular mysql functions inside WordPress..

    I've been playing with some code and come up with this for updating the table for you....

    When a post or page is saved 2 queries are performed (it would have been several using a loop).

    First a query is done to grab the tag ID and slug, then a second that updates all the necessary entries in the secondary table (definable at the top)... Matches are made by slug, and the ID updated...

    For the second query i used some clever php/mysql to make the update work in one single query. However this may not work on some server configurations, so you'll have to try it and see (it will save looping over each tag and doing one query per tag)...

    Took a little bit of testing, but it's working on my test install...

    Update query is based on the information gave earlier....

    Hope that helps.. :)

  23. Jim R
    Posted 6 years ago #

    You can use regular MySQL functions inside WP, at least through PHP. I'm using them in a couple of places, just standard Select commands. I was able to do it with my custom table in its own database, but that did create conflicts with some widgets. Moving the table to my WP database eliminated those conflicts.

    Which file are you calling or putting that function?

    Where are you defining which table to Update? I've been testing the on the backup one. I'd need to change it eventually.

  24. Mark / t31os

    Posted 6 years ago #

    Just create a file in the plugins folder..

    Call it anything you like (with a .php extension), then plonk the code in, rename the value next to S_DB, so "wp_test" was my test table (rename that to suit)..

    It's not perfect, but i was eager to see if i could do it.. the bottom query was interesting, not used the "case" statement in mysql queries before....(googled for "php mysql update multiple rows single query")..

    Let me know how it works out for you... :)

  25. Jim R
    Posted 6 years ago #

    And it's just going to work? I don't have to call the function or include the file anywhere? :)

  26. Mark / t31os

    Posted 6 years ago #

    Other then activating it yes that's it..

    You'll have to adjust it in the necessary areas.. but yes for the most part i've just saved you all the trouble of writing one... ;)

    I'm not a developer though, so i'll make no guarantees about how well it will work and whether the methods are safe....

    I've given you a starting point if it needs improvement in any case.. :)

  27. Jim R
    Posted 6 years ago #

    I created the file and saved it in the plugins folder. It's not Updating my custom table with the Tag ID in the appropriate row.

  28. Jim R
    Posted 6 years ago #

    Edited this entry.

  29. Jim R
    Posted 6 years ago #

    Uhm...yeah....it worked. I forgot to activate it. :)

    The more I looked at it, the more the code made sense. I get stuck with all the variables. I was thinking of digging in tomorrow and making a function, and it dawned on me that I had not activated it. I was trying to figure out how the plugin worked since I didn't a-c-t-i-v-a-t-e it...DOH!!!

    Pretty cool. Pretty impressive. Thanks!

  30. Mark / t31os

    Posted 6 years ago #

    Glad i could help.. :)

Topic Closed

This topic has been closed to new replies.

About this Topic