Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MySQL查询Wordpress中的唯一slug_Php_Mysql_Wordpress_Slug - Fatal编程技术网

Php MySQL查询Wordpress中的唯一slug

Php MySQL查询Wordpress中的唯一slug,php,mysql,wordpress,slug,Php,Mysql,Wordpress,Slug,我想要一个MySQL查询来查找和替换wordpress数据库中的关键字。并且想保持鼻涕虫的独特性。例如:如果wp\u terms表中有3个slug:- testkeyword testkeyword-2 testslg 现在我想将关键字slg替换为keyword,并按如下方式运行更新查询 update wp_terms set `slug` = replace(slug, 'slg','keyword') 现在第三个slug是testslg,它将成为testkeyword,并且它将不会保持唯一

我想要一个MySQL查询来查找和替换wordpress数据库中的关键字。并且想保持鼻涕虫的独特性。例如:如果
wp\u terms
表中有3个slug:-

  • testkeyword
  • testkeyword-2
  • testslg
  • 现在我想将关键字
    slg
    替换为
    keyword
    ,并按如下方式运行更新查询

    update wp_terms set `slug` = replace(slug, 'slg','keyword')
    
    现在第三个slug是
    testslg
    ,它将成为
    testkeyword
    ,并且它将不会保持唯一,因为第一个slug与第三个slug相同

    如果slug已经存在于列中,我如何在其末尾添加数字?
    在本例中,我希望第三个slug是
    testkeyword-3
    。这是怎么可能的?

    首先,获取新形成的关键字的初始计数。假设使用下面的语句将第三个关键字testslg替换为testkeyword

    $newkeyword = str_replace("slg","keyword","testslg");
    
    现在,使用以下查询获取这个newkeyword的计数

    SELECT count(slug) FROM wp_terms WHERE slug like '%$newkeyword%';
    
    在更新查询中使用此计数

    update wp_terms set `slug` = '$newkeyword-$above_query_count_result' WHERE row_unique_id;
    

    注意:这个答案只是逻辑,请在php代码中包含所有连接。

    首先获取新形成关键字的初始计数。假设使用下面的语句将第三个关键字testslg替换为testkeyword

    $newkeyword = str_replace("slg","keyword","testslg");
    
    现在,使用以下查询获取这个newkeyword的计数

    SELECT count(slug) FROM wp_terms WHERE slug like '%$newkeyword%';
    
    在更新查询中使用此计数

    update wp_terms set `slug` = '$newkeyword-$above_query_count_result' WHERE row_unique_id;
    

    注意:这个答案只是逻辑,请在php代码中包含所有连接。

    Hi,感谢您的回答,但在我的示例中,“testslg”是数据库中的一部分,查询将只包含要查找的关键字(slg)和要替换的关键字(关键字)。所以我认为不可能使用这个str_替换(“slg”,“关键字”,“testslg”);您好,谢谢您的回答,但在我的示例中,“testslg”是数据库中的一部分,查询将只包含要查找的关键字(slg)和要替换的关键字(keyword)。所以我认为不可能使用这个str_替换(“slg”,“关键字”,“testslg”);