0

I'm building a simple post order "plugin" through my theme's functions.php. The function updates the value of an option in options.php on submission of a form. So, for instance, when "1" is inputted in the field next to a post in a WP_Query, the ID of that post is sent to an option in options.php called "post1". So the option is "post1" and the value is "1456" (the ID of the post). If "2" is inputted in the field, it sends the ID of the post to an option called "post2". I would have thought this is fairly straightforward.

At the start of the script, I call in all the options such that

$posts1 = get_option('post1');
$posts2 = get_option('post2');

and so on, such that $posts1, and $posts2 are IDs of posts. I then pull these into an array which then goes into a WP_Query. This is so that the user knows what the current order of the posts is.

Below then is another WP_Query that does all the aforementioned magic above of pulling the ID of the post into the option depending on what number the user selects when the form is submitted.

What I would have expected is that upon submission of the form, where the options are changed (and they are changed, because I check in options.php each time), the reload of the page would present the newer list above – as in, with the options changed. However, what appears in the first WP_Query, which is pulling in the options as above and sending them to the array, is that the older options are being pulled in by the script.

I had thought maybe that the script was still working after submission and the options hadn't been changed in time for the page load, so I put a sleep(10); before the starting get_option list, but still, the older options are pulled in even after ten seconds.

If I immediately refresh the page after the original submission and reload of the page, the new options are pulled in – just not on the original submission and reload.

Why is this, and how can I fix it? Are there reasons why the script seems to be pulling in options that seem to be like cached or something?

Thanks for your help.

To reiterate, I am aware that the original get_option call is occurring before the update_option, but the update_option only occurs upon submission of the form, so the page is reloaded. The new options should be presented because the page is reloaded after the update_option occurs, but they don't. I want the "old" options to appear on page load initially, so the user can see what the current order of the posts is, can update them, submit the form, then the page reloads, and they should be presented with the options they have just inputted.

Here is a simplified version of the code (which I realise is inefficient and clumsy, but I don't think that should affect the issue at hand):

     <?php function functionpostorderit() {     ?>

          <?php

/* THE FIRST LOOP BELOW IS SO USERS CAN SEE THE CURRENT POST ORDER */          

    global $post;
    global $my_query;



            $posts1 = get_option('post1');
            $posts2 = get_option('post2');
            $posts3 = get_option('post3');


            $post_listids = array($posts1,$posts2,$posts3);

            array_filter($post_listids);



            $paged_l = 1;
            $posts_per_page_l = 25;
            $offset = ( $paged_l - 1 ) * $posts_per_page_l;
            $ids_to_query_l = array_slice( $post_listids, $offset, $posts_per_page_l );




        $my_query = new WP_Query( array('post_type' => array( 'post', 'feature' ), 'post__in' => $ids_to_query_l, 'posts_per_page' => $posts_per_page_l, 'orderby' => 'post__in', 'ignore_sticky_posts' => 1, 'post_status' => 'publish') );



        if($my_query->have_posts()) {

        echo '<ul>';

        $loop_counter = 0;


        while ( $my_query->have_posts() ) {
            $my_query->the_post();

            $loop_counter++;



    echo '<li>'.get_the_title() . '<input type="text" name="number_of_'.$loop_counter.'" value="" />'.'</li>' ;




    }
        echo '</ul>';



    } 



    else {

    }


    wp_reset_postdata();  


    ?>



    <hr />



    <form method="post" action="">



                <?php


          global $post;
    global $my_query;


/* THE SECOND LOOP BELOW IS THE ONE IN WHICH USERS PRIMARILY UPDATE THE ORDER */        


        $my_query = new WP_Query( array('post_type' => array( 'post', 'feature' ), 'posts_per_page' => 25, 'orderby' => 'date', 'post_status' => 'publish', 'post__not_in' => $post_listids  
    ) );



        if($my_query->have_posts()) {

        echo '<ul>';

        $loop_counter = 0;


        while ( $my_query->have_posts() ) {
            $my_query->the_post();

            $loop_counter++;


    $this_id = get_the_ID();


    echo '<li>' . get_the_title() . '<input type="text" name="number_of_'.$loop_counter.'" value="" />'.$this_id.'</li>' ;

    ${'id_of_' . $loop_counter} = get_the_ID();

    $current_id = get_the_ID();

    ${'number_of_' . $loop_counter} = $_POST['number_of_' . $loop_counter];

    $current_value_input = $_POST['number_of_' . $loop_counter];





    if(isset($_POST['submit'])) 
       {


    $current_number_of = 'number_of_'.$loop_counter;






           if($current_value_input !== '') {


                 $checking_for_equal = array('number_of_1','number_of_2','number_of_3');


    $remove_equal = array_search('number_of_'.$loop_counter, $checking_for_equal);

    unset($checking_for_equal[$remove_equal]);


     foreach ($checking_for_equal as &$valued) {
        $valued = $_POST[$valued];

    }

    if (in_array(${'number_of_' . $loop_counter}, $checking_for_equal))
      {

      if ($has_run !== "true") {

        echo "<div id='message' class='error'>Sorry, posts cannot have the same position. Try again.</div>";

        $has_run = "true";

        }




      }
    else
      {      


                if($posts1 == $current_id) {

                    $option = 'post1';

                    $new_value = '';

                    update_option( $option, $new_value );


                }


                elseif($posts2 == $current_id) {

                    $option = 'post2';

                    $new_value = '';

                    update_option( $option, $new_value );


                }


                elseif($posts3 == $current_id) {

                    $option = 'post3';

                    $new_value = '';

                    update_option( $option, $new_value );


                }




                else {



                }



          $option = 'post'.${'number_of_' . $loop_counter};

          $new_value = ${'id_of_' . $loop_counter};

          update_option( $option, $new_value );




          }





          }




       }






    }
        echo '</ul>';
    } else {

    }

    wp_reset_postdata();  




    ?>


         <p><input class="submitter" type="submit" name="submit" value="Update" /></p>


            </form>



    <?php
    }
    ?>
  • Can you post the code? – Tomás Cot Sep 1 '14 at 17:47
  • I can, but I think people will point out the multitude of inefficiencies I have in it rather than the actual problem I am addressing. – Edmund Heaphy Sep 1 '14 at 20:05
  • if you check your code it seems that you have javascript that you aren't echoing – Tomás Cot Sep 2 '14 at 2:15
1

If your update_option call runs after you assign $posts1 and $posts2, then of course they'll hold the "old" value, even if you sleep for eternity ;)

Either run the update earlier, or (re)assign the variables afterwards.

$posts1 = get_option( 'post1' );

echo get_option( 'post1' ); // [my_id]
echo $posts1; // [my_id]

update_option( 'post1', 'foobar' );

echo get_option( 'post1' ); // foobar
echo $posts1; // [my_id]

See how $posts1 gets left out of the update?

Thus, the script runs again, and (I think) the new values from the submission should now be displayed at the top. But it's the old ones.

Because when a user POST's the form the script simply runs again, but the values aren't updated till the end of the script. I can see the behaviour you're after, and trust me, it will function as intended if you place the update logic at the start of the script.

  • No, but the new values don't appear (instead, the old ones) after the form is submitted and the page is refreshed. Is it the case that after a form is submitted, the page is not actually refreshed? If so, how do I get around this? Like, I want the old values to appear on the page so the user can see the current order. Then they update them below, submit, and the page is refreshed (or appears to refresh), so I thought the newly updated ones would appear. – Edmund Heaphy Sep 1 '14 at 20:04
  • WP's internal cache mechanism means that as soon as you run update_option, calling get_option will get the new value immediately. Page refreshing isn't really relevant here (though it's a nice technique). I've updated my answer to demonstrate. We'll solve it, don't worry! – TheDeadMedic Sep 1 '14 at 20:10
  • Apologies if I seem annoying, and thank you so so much for your help, but I think you may be missing the point. I understand that the update_option occurs after the initial pile of get_option requests, and thus the "old" value is displayed. I want it that way, because before, the user can see how it stands at that moment, and then update the values, which only happens on submission of a form, which refreshes the page. Thus, the script runs again, and (I think) the new values from the submission should now be displayed at the top. But it's the old ones. Does this make sense? Thanks again. – Edmund Heaphy Sep 1 '14 at 20:34
  • I have added my code and further explanation. – Edmund Heaphy Sep 1 '14 at 20:45
  • No worries, see my comment as an update to my answer. – TheDeadMedic Sep 2 '14 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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