Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
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 '12 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 '12 at 22:11
add comment

1 Answer

up vote 0 down vote accepted

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'")
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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