将字符串与wpdb中的帖子ID进行比较,并在找到匹配项时执行操作

时间:2012-10-07 作者:INT

我正在寻找一些与wpdb相关的帮助,以比较和更改某些值。我是mysql处理方面的新手,所以我非常感谢您在这方面的帮助!

我想做的是通过wp db查找与$page_id. 找到帖子后,应检查menu_order 值并将其与$page_order. 如果它们相同,则什么也不做,但如果它们不匹配,则应设置menu_order 价值$page_order.

下面是一些伪代码:

$page_id = 45;
$page_order = 1;

if(post_id == $page_id) {
    if(menu_order != $page_order) {
        set menu_order to $page_order

        switch($page_order) {
        case "1":
            set post meta "color_dropdown" as "pink"
                if have child pages set post meta "color_dropdown" as "pink" to them aswell
            break;
        }
    }
}
感谢所有帮助!

谢谢

1 个回复
最合适的回答,由SO网友:INT 整理而成

这就是我最终使用的,因为我使用了另一种方法,所以我不需要将元值应用到子页面。

希望它对某人有用。

$row = $wpdb->get_row("SELECT * FROM wp_posts WHERE ID =\'$post_id\'");
$new_order = $order[$page[\'depth\']];
$new_order = $wpdb->escape($new_order);
$current_order = $row->menu_order;

if($current_order != $new_order) {
    switch ($new_order) {
        case "1":
            $color = \'pink\';
            break;
        case "2":
            $color = \'green\';
            break;
        case "3":
            $color = \'turquoise\';
            break;
        case "4":
            $color = \'brown\';
            break;
        case "5":
            $color = \'orange\';
            break;
        case "6":
            $color = \'pink\';
            break;
        case "7":
            $color = \'green\';
            break;
        case "8":
            $color = \'turquoise\';
            break;
        case "9":
            $color = \'brown\';
            break;
        }
        $color = $wpdb->escape($color);
        $post_id = $wpdb->escape($post_id);
        $wpdb->query("UPDATE wp_postmeta SET meta_value = \'$color\' WHERE post_id = \'$post_id\' AND meta_key = \'color_dropdown\'")
}

结束