AgileAdam.com

  • Home
  • Development
  • Tech Tips
  • Worth Automating?
  • Woodturnings
  • Photography
  • Home
  • Development
  • Tech Tips
  • Worth Automating?
  • Woodturnings
  • Photography
  • Development

    Update Field Value for All Nodes

    / March 3, 2016

    I recently added a “Show in Listing” boolean on/off checkbox field to a content type (default value is to be “checked”). There were 133 nodes when I created that field. I needed to update all of those nodes to be “checked”.

    Here’s how I updated all of them using entity_metadata_wrapper(). It’s much quicker than any alternative I could think of (feeds, rules, etc.).

    PHP
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    $nids = db_query('SELECT nid FROM {node} WHERE type = :type', array(':type' => 'blogpost'))->fetchCol();
    foreach ($nids as $nid) {
      $node = node_load($nid);
      if ($node) {
        $wrapper = entity_metadata_wrapper('node', $node);
        if (isset($wrapper->field_islisted)) {
          $wrapper->field_islisted->set(1);
          $wrapper->save();
        }
      }
    }

    In this particular case I’m targeting a single content type. I could’ve used a similar query and some slight modifications to set the value on any node whose bundle (content type) supports that field_islisted field.

    Lastly, if you have a lot of nodes, you might consider using node_load_multiple()  instead of node_load() .

    read more
  • Development

    Copying Values Between Fields on Thousands of Entities

    / January 30, 2014

    In a nutshell, I had a List (text) field that I needed to convert to a Text field (so that I wasn’t locked in to specific values anymore). Instead of trying to modify the field to convert it from one type to another, I decided to create a new Text field and populate it with the data from the List field (modifying it a bit first). Here’s the script I used to do this. Admittedly it could be cleaner, more efficient, etc. but it’ll show you a few things, and it got the job done for me.

    If you choose to run with drush scr update_nodes.php you can leave out the top 3 bootstrap lines.

    read more

Recent Posts

  • You may want to disable “Preferred Activities” on your Google Nest Wifi
  • Wooshy + Keyboard Maestro = Incredible Power
  • Using Dataview with Charts in Obsidian
  • Obsidian Daily Note Implementation v2
  • Clipboard Manipulation using Keyboard Maestro

Archives

  • November 2022
  • August 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • October 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • March 2021
  • February 2021
  • December 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • August 2019
  • July 2019
  • June 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • January 2018
  • December 2017
  • November 2017
  • September 2017
  • August 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • November 2015
  • October 2015
  • September 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • September 2013
  • December 2012
  • August 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • November 2010

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
Ashe Theme by WP Royal.