Wp post meta patch update (WP-CLI)

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen

wp post meta patch update: Update a nested meta field.

NOTE: This command can take an arbitrary number of arguments (key-path) and can take a value from standard input ⇒ When using this in a Bash loop, that goes through the content of a file, the content of the Bash loop will be interpreted as the standard input value - See below

The help text is the same as for wp post meta patch:

$wp help post meta patch update

NAME

  wp post meta patch

DESCRIPTION

  Update a nested value for a meta field.

SYNOPSIS

  wp post meta patch <action> <id> <key> <key-path>... [<value>] [--format=<format>]

OPTIONS

  <action>
    Patch action to perform.
    ---
    options:
      - insert
      - update
      - delete
    ---

  <id>
    The ID of the object.

  <key>
    The name of the meta field to update.

  <key-path>...
    The name(s) of the keys within the value to locate the value to patch.

  [<value>]
    The new value. If omitted, the value is read from STDIN.

  [--format=<format>]
    The serialization format for the value.
    ---
    default: plaintext
    options:
      - plaintext
      - json
    ---

Basic example

This example is a continuation from a similar example at wp post meta get and wp post meta pluck. This is a relatively simple situation as hero_ctas_0_link consists of only 1 level with 3 nested fields.

################################################################################
# hero_ctas_0_link
################################################################################
#
# * "hero_ctas_0_link" is the first field with serialised content, when you use
#   "wp post meta list 7"
# * This is a relatively easy field: Only 1 level with 3 nested fields
# * The usual buttonbarbutton URL field, is more complex (see elsewhere)
#
#
# OK - "list"
########################################
#
# wp post meta list 7
# wp post meta list 7 --keys="hero_ctas_0_link"
#
#
# OK - "get"
########################################
#
# wp post meta get 7 "hero_ctas_0_link"	# OK
# wp post meta get 7 hero_ctas_0_link	# OK
#
#
# Output of any of these last get statements:
#
# array (
#   'title' => 'Bekijk alle widgets',
#   'url' => 'http://en.s1/shop/',
#   'target' => '',
# )
#
#
# OK - "pluck"
########################################
#
# wp post meta pluck 7 hero_ctas_0_link         # Error: Compound object
# wp post meta pluck 7 hero_ctas_0_link title   # OK
# wp post meta pluck 7 hero_ctas_0_link url     # OK
# wp post meta pluck 7 hero_ctas_0_link target  # OK
#
#
# Output:
#
# Bekijk alle widgets
# http://en.s1/shop/
# (empty line)
#
#
# OK - "patch update" - title field
########################################
#
# wp post meta patch update 7 hero_ctas_0_link title "this_is_updated_title" # OK
# wp post meta get 7 hero_ctas_0_link
#
# wp post meta patch update 7 hero_ctas_0_link title "title with spaces" # OK
# wp post meta get 7 hero_ctas_0_link
#
# title="title_without_space"
# wp post meta patch update 7 hero_ctas_0_link title $title   # OK
# wp post meta patch update 7 hero_ctas_0_link title "$title" # OK
# wp post meta get 7 hero_ctas_0_link
#
# title="title with spaces"
# wp post meta patch update 7 hero_ctas_0_link title $title   # Error due to spaces
# wp post meta patch update 7 hero_ctas_0_link title "$title" # OK
# wp post meta get 7 hero_ctas_0_link
#
#
# OK - "patch update" - url field
########################################
#
# URLs don't contain spaces, so that case doesn't need to be tested
#
# wp post meta patch update 7 hero_ctas_0_link url "http://en.s1/shop-2/" # OK
# wp post meta get 7 hero_ctas_0_link
#
# url="http://en.s1/shop-3/"
# wp post meta patch update 7 hero_ctas_0_link url $url # OK
# wp post meta get 7 hero_ctas_0_link
#
# url="http://en.s1/shop-4/"
# wp post meta patch update 7 hero_ctas_0_link url "$url" # OK
# wp post meta get 7 hero_ctas_0_link

Buttonbar button example

This is what I had some major issues with, just some days ago - And now it's all smooth sailing.

################################################################################
# buttonbar_selection_0_link
################################################################################
#
# * This is the first buttonbar button on the home page
# * It's actually similar in complexity to the example before: 1 Level with
#   just 3 nested values.
#
#
# OK - "list"
########################################
#
# wp post meta list 7   # OK
# wp post meta list 7 --keys="buttonbar_selection_0_link"   # OK
#
#
# OK - "get"
########################################
#
# wp post meta get 7 buttonbar_selection_0_link   # OK
#
# Output:
#
# array (
#   'title' => '',
#   'url' => 'http://en.s1/aeg/',
#   'target' => '',
# )
#
#
# OK - "pluck"
########################################
#
# wp post meta pluck 7 buttonbar_selection_0_link title		# OK
# wp post meta pluck 7 buttonbar_selection_0_link url 		# OK
# wp post meta pluck 7 buttonbar_selection_0_link target 	# OK
#
# Output:
#
# (empty line)
# http://en.s1/aeg/
# (empty line)
#
#
# OK - "patch update" - title field
########################################
#
# wp post meta patch update 7 buttonbar_selection_0_link title "title_01" # OK
# wp post meta get 7 buttonbar_selection_0_link
#
# wp post meta patch update 7 buttonbar_selection_0_link title "title 02" # OK
# wp post meta get 7 buttonbar_selection_0_link
#
# title="title_03"
# wp post meta patch update 7 buttonbar_selection_0_link title "$title"   # OK
# wp post meta get 7 buttonbar_selection_0_link
#
# title="title 04"
# wp post meta patch update 7 buttonbar_selection_0_link title "$title"   # OK
# wp post meta get 7 buttonbar_selection_0_link
#
#
# OK - "patch update" - url field
########################################
#
# I had some major trouble with this recently, but now everything seems fine
# and as expected
#
# wp post meta patch update 7 buttonbar_selection_0_link url "http://en.s1/aeg2/" # OK
# wp post meta get 7 buttonbar_selection_0_link
#
# url="http://en.s1/aeg3/"
# wp post meta patch update 7 buttonbar_selection_0_link url "$url"	# OK
# wp post meta get 7 buttonbar_selection_0_link
#
# post_id=7
# object01="buttonbar_selection_0_link"
# object02="url"
# url="http://en.s1/aeg6/"
#
# wp post meta patch update $post_id $object01 $object02 "$url"			# OK
# wp post meta patch update "$post_id" $object01 $object02 "$url"		# OK
# wp post meta patch update "$post_id" "$object01" "$object02" "$url"	# OK
# wp post meta get 7 buttonbar_selection_0_link
#
#
# OK - url field with special values?
########################################
#
# To WordPress, the nested value is just a string, so it doesn't get about
# spaces or other things that would be incorrect in URLs
#
# post_id=7
# object01="buttonbar_selection_0_link"
# object02="url"
# url="http://en.s1/aeg6/?q=hoi"
# #
# wp post meta patch update $post_id    $object01   $object02  "$url"	# OK
# wp post meta patch update "$post_id"  $object01   $object02  "$url"	# OK
# wp post meta patch update "$post_id" "$object01" "$object02" "$url"	# OK
# wp post meta get 7 buttonbar_selection_0_link
#
#
# post_id=7
# object01="buttonbar_selection_0_link"
# object02="url"
# url="http://en.s1/aeg6/ / "
# #
# wp post meta patch update $post_id    $object01   $object02  "$url"	# OK
# wp post meta patch update "$post_id"  $object01   $object02  "$url"	# OK
# wp post meta patch update "$post_id" "$object01" "$object02" "$url"	# OK
# wp post meta get 7 buttonbar_selection_0_link
#
#
# OK - title field with special values?
########################################
#
# post_id=7
# object01="buttonbar_selection_0_link"
# object02="title"
# title='He said "hoi"?'
# #
# wp post meta patch update $post_id    $object01   $object02  "$title"	# OK
# wp post meta patch update "$post_id"  $object01   $object02  "$title"	# OK
# wp post meta patch update "$post_id" "$object01" "$object02" "$title"	# OK
# wp post meta get 7 buttonbar_selection_0_link

Problem with Bash loop through content

This command can take an arbitrary number of arguments (key-path) and can take a value from standard input ⇒ When using this in a Bash loop, that goes through the content of a file, the content will be interpreted as the standard input value, causing errors like:

Error: No data exists for key "http://en.s1/aeg6/"

whereby the cited string is actually the value and not the last part of the key-path as how it is interpreted here.

This problem reproduced

#!/bin/bash
#
# Problem with "wp post meta patch update" reproduced
################################################################################
#
#
################################################################################
# Prologue
################################################################################
#
echo ""; echo ""
echo "########################################################################################"
echo "# wp_slash_problem_reproduced.sh"
echo "########################################################################################"
echo ""


################################################################################
# collect_meta_posts_with_spaces_inside_urls()
################################################################################
#
collect_meta_posts_with_spaces_inside_urls()
{
   #
   sql_query='select post_id, meta_id, meta_key from wp_postmeta '
   sql_query+='join wp_posts on post_id=ID where '
   sql_query+='meta_key like "buttonbar%selection%link" and '
   sql_query+='meta_value like "% /%" and post_type="page"'

   wp db query "$sql_query" --skip-column-names > /tmp/data_output.csv
   #
}


################################################################################
# fix_spaces_inside_urls
################################################################################
#
fix_spaces_inside_urls()
{
   #
   # Read and process the file line by line
   ########################################
   #
   while read -r post_id meta_id meta_key
   do
      #
      # Retrieve & echo stuff
      ########################################
      #
      echo ""
      echo "########################################"
      echo "   post_id: $post_id"
      echo "   meta_key: $meta_key"
      url=$(wp post meta pluck $post_id $meta_key "url")
      echo "   url: $url"


      # Update meta field - NOT OK
      ########################################
      #
      # Here it is where the current line from "/tmp/data_output.csv"
      # gets appened to this command
      #
      wp post meta patch update $post_id $meta_key url "$url"
      #
      #
   done < "/tmp/data_output.csv"
   #
}


################################################################################
# MAIN
################################################################################
#
cd /var/www/en.s1
collect_meta_posts_with_spaces_inside_urls
fix_spaces_inside_urls

Solution

Just a couple of bytes can make such a difference:

#!/bin/bash
#
# Problem with "wp post meta patch update" reproduced
################################################################################
#
#
################################################################################
# Prologue
################################################################################
#
echo ""; echo ""
echo "########################################################################################"
echo "# wp_slash_problem_reproduced.sh"
echo "########################################################################################"
echo ""


################################################################################
# collect_meta_posts_with_spaces_inside_urls()
################################################################################
#
collect_meta_posts_with_spaces_inside_urls()
{
   #
   sql_query='select post_id, meta_id, meta_key from wp_postmeta '
   sql_query+='join wp_posts on post_id=ID where '
   sql_query+='meta_key like "buttonbar%selection%link" and '
   sql_query+='meta_value like "% /%" and post_type="page"'

   wp db query "$sql_query" --skip-column-names > /tmp/data_output.csv
   #
}


################################################################################
# fix_spaces_inside_urls
################################################################################
#
fix_spaces_inside_urls()
{
   #
   # Read and process the file line by line
   ########################################
   #
   while read -u 10 post_id meta_id meta_key
   do
      #
      # Retrieve & echo stuff
      ########################################
      #
      echo ""
      echo "########################################"
      echo "   post_id: $post_id"
      echo "   meta_key: $meta_key"
      url=$(wp post meta pluck $post_id $meta_key "url")
      echo "   url: $url"


      # Update meta field - NOT OK
      ########################################
      #
      # Here it is where the current line from "/tmp/data_output.csv"
      # gets appened to this command
      #
      wp post meta patch update $post_id $meta_key url "$url"
      #
      #
   done 10< "/tmp/data_output.csv"
   #
}


################################################################################
# MAIN
################################################################################
#
cd /var/www/en.s1
collect_meta_posts_with_spaces_inside_urls
fix_spaces_inside_urls

See also

Sources