1

I'm looking for some help tying in with the wpdb to compare and change certain values. I'm very novice with mysql handling so I really appreciate any help on this matter!

What I want to do is to go through the wp db looking for a post same id as $page_id. When the post is found it should check the menu_order value and compare it to $page_order. If they're the same, then do nothing, but if they don't match it should set menu_order to the value of $page_order.

Here's some pseudo code:

$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;
        }
    }
}

All help appreciated!

Thanks

2
  • Where and when would you run this code? As a cron? It might have negative effects for the editors since you would effectively change the menu order which they set. If you want to display the same order as page order you can always wp_list_pages
    – Miha Rekar
    Oct 7, 2012 at 20:18
  • @MihaRekar It's a custom CPT sorter based on the plugin CMS Page Order. I tie in with the ajax functionality that saves the new order in that plugin. I just want to be able to set post meta based on certain conditions. Can you help?
    – INT
    Oct 7, 2012 at 22:11

1 Answer 1

0

This is what I ended up using, turned out I didn't need to apply the meta value to the child pages since I used another approach.

Hopefully it's useful for someone.

$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'")
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.