【问题标题】:How to get the page_ids from a list of page_slugs from WordPress and store them as a string?如何从 WordPress 的 page_slugs 列表中获取 page_ids 并将它们存储为字符串?
【发布时间】:2014-09-03 14:44:32
【问题描述】:

我想做的是创建一个 wordpress 函数来从 page_slugs 列表中获取 page_ids 并将它们作为字符串存储在一个变量中,然后我可以从另一个函数内部简单地“回显”/“调用”。

到目前为止,我可以通过以下方式从 page_slug 获取单个 page_id:

$the_page_slug = 'test';
global $wpdb;
$page_id_from_slug = '\'' . $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name ='".$the_page_slug."'") . '\'';

echo $page_id_from_slug;

result is ok: '12'

这个功能也有效

function id_from_slug($the_page_slug){
global $wpdb;
$page_id_from_slug = '\'' . $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name ='".$the_page_slug."'") . '\'';
return $page_id_from_slug;
}
echo id_from_slug('test');

result is ok: '12'

问题是我无法让它与一组 page_slugs 一起工作。

我的数据是这样的:

$the_page_slugs = array('test', 'test-nr4', 'test-9', 'sample4', 'sample-nr12');

回显输出(page_ids)应该是这样的:

“12”、“16”、“54”、“76”、“123”

有什么想法吗?

【问题讨论】:

    标签: php arrays wordpress


    【解决方案1】:

    所以这是我的解决方案,工作正常,正是我需要的。 查看代码,看看我更改/添加了什么:

    IN() 和 IMPLODE() 提示确实是我所需要的,在将“get_vars”(仅获得 ONE 结果)更改为“get_results”并稍作调整后它就起作用了。

    这是我的代码,肯定可以改进...

            // code to get page_ids from page_slugs
            // the list of page_slugs
    $the_page_slugs = array('test', 'test-re', '1234', 'test-56', 'sample34', 'me3-uc', '1-12987-db', 'thy-m2o-1873');
            // set global, just in case ;)
    global $wpdb;
            // query to get the page_ids
    $sql = "SELECT ID FROM wp_posts WHERE post_name IN('" . implode("', '", $the_page_slugs) . "') AND post_status = 'publish'";
            //this calls the query
    $get_page_ids = $wpdb->get_results($wpdb->prepare($sql));
            //prepare new var
    $the_page_ids ='';
            //loop thru results to create var
    foreach($get_page_ids as $get_page_ids_result){
            //formate results, need it comma separated
        $the_page_ids .= ('' == $the_page_ids ) ? '':', ';
            //fill it
        $the_page_ids .= $get_page_ids_result->ID;
        }
    echo "check if result is what we need<br/>";
    echo $the_page_ids;
            //perfekt result, looks like: 325, 323, 324, 327, 328, 329, 334, 335
    

    感谢您的帮助。

    【讨论】:

      【解决方案2】:

      试试下面的方法。

      global $wpdb;
      
      $the_page_slugs = array('test', 'test-nr4', 'test-9', 'sample4', 'sample-nr12');
      $page_id_from_slug = '\'' . $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name in '" . implode("','",$the_page_slugs) . "'") . '\'';
      
      echo $page_id_from_slug;
      

      【讨论】:

      • 谢谢,这是一个很好的起点。主要问题是 get_var 只带回一个结果。顺便说一下,您错过了 IN 括号;)
      【解决方案3】:

      把你的 sql 改成:

      "SELECT ID FROM $wpdb->posts WHERE post_name in (".implode(",",$the_page_slugs)."))";
      

      从数据库中获取到print_r后会有一系列成功记录。

      【讨论】:

      • 不错,并没有真正为我工作,但给了我一个“提示”我所缺少的东西。谢谢
      猜你喜欢
      • 2014-09-13
      • 2013-06-25
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2020-10-11
      • 1970-01-01
      相关资源
      最近更新 更多