wp-cli / shell-command

Opens an interactive PHP console for running and testing PHP code.
MIT License
20 stars 16 forks source link

New data is not available when updated from another session #13

Closed bor0 closed 7 years ago

bor0 commented 7 years ago

Steps to reproduce:

  1. Execute these commands:
    wp> get_post_meta( 139, '_price', true )
    => phar:///Users/boro/bin/wp/php/WP_CLI/REPL.php:43:
    string(3) "100"
    wp> update_post_meta( 139, '_price', 200 )
    => phar:///Users/boro/bin/wp/php/WP_CLI/REPL.php:43:
    bool(true)
    wp> get_post_meta( 139, '_price', true )
    => phar:///Users/boro/bin/wp/php/WP_CLI/REPL.php:43:
    string(3) "200"
  2. Leave the wp shell running in background, and in a different session run this DB query:
    mysql> update wp_postmeta set meta_value = '123' where post_id = 139 and meta_key = '_price';
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
  3. Go back to the wp shell, and run this:
    wp> get_post_meta( 139, '_price', true )
    => phar:///Users/boro/bin/wp/php/WP_CLI/REPL.php:43:
    string(3) "200"
    wp> 

If you close and open wp shell again, it will return the correct value:

boro@bor0:~/dev/www/wordpress$ wp shell
wp> get_post_meta( 139, '_price', true )
=> phar:///Users/boro/bin/wp/php/WP_CLI/REPL.php:43:
string(3) "123"
wp> 
schlessera commented 7 years ago

@bor0 : This is the expected behavior for WordPress. The function get_post_meta() runs through the WP Object Cache, so that it is only fetched once from the database within a given PHP process/request. WordPress is meant to be shutdown and restarted after every request, so this behavior rarely causes an issue.

In your wp shell process, however, we start one single WP process and then keep that one going. This is why you're still hitting the cache with subsequent queries. They never hit the database, the information is already present in memory and fetched from there.

You can either flush the cache within the existing process yourself with wp_cache_flush(), or restart the shell.

Related issue: https://github.com/wp-cli/ideas/issues/63

bor0 commented 7 years ago

Thanks @schlessera. I called wp_suspend_cache_addition( true ); at the beginning of the shell and it works now. I thought it was a part of wp shell.

cc @gedex