Title: Using WP_query() in function.php
Last modified: August 24, 2016

---

# Using WP_query() in function.php

 *  [Lzhelenin](https://wordpress.org/support/users/lzhelenin/)
 * (@lzhelenin)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/using-wp_query-in-functionphp/)
 * Hello guys, I got stuck with subj. I wrote a function that gets certain fields
   of posts (the ‘advanced custom fields’ plugin was used for the fields) with custom
   type ‘opt’, pushes it in an array, encodes it to json and updates .json file.
 *     ```
       function opt_update() {
       $args = array(
           'post_type' => 'opt'
       );
       $opt_array = array();
       $the_query = new WP_Query($args);
       while ($the_query -> have_posts()): $the_query -> the_post();
       $good_type_array = array();
       while (have_rows('type')): the_row();
       $type_name = get_sub_field('type_name');
       array_push($good_type_array, $type_name);
       endwhile;
       array_push($opt_array, array(
           'name' => get_the_title(),
           'good_type' => $good_type_array,
           'price' => get_field('price')
       ));
       endwhile;
       wp_reset_postdata();
       file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/blablabla/json/data.json', raw_json_encode($opt_array));}
   
       function raw_json_encode($input) {
           return preg_replace_callback(
               '/\\\\u([0-9a-zA-Z]{4})/',
               function ($matches) {
                   return mb_convert_encoding(pack('H*',$matches[1]),'UTF-8','UTF-16');
               },
               json_encode($input)
           );
       }
       ```
   
 * It works perfectly if I call the function, for example, on the main page:
 *     ```
       opt_update();
       ```
   
 * But what I need is to execute the function when I publish/update the ‘opt’ custom
   post. I tried to use that hook:
 *     ```
       add_action('publish_opt', 'opt_update', 10, 2);
       ```
   
 * … but it doesn’t work at all. Although it’s not a problem of the hook, if I put
   instead of ‘opt_update’ an another function, for example:
 *     ```
       add_action('publish_opt', 'mail_me', 10, 2);
       ```
   
 * … it sends me an e-mail message as it is supposed to. Where am I wrong?

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [WordPress Virus](https://wordpress.org/support/users/navneilnaicker/)
 * (@navneilnaicker)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/using-wp_query-in-functionphp/#post-6057563)
 * If am not wrong then, this should work for you. You are on the right track but
   few things needs to change in your code.
 * Checkout the Codex
    [https://codex.wordpress.org/Post_Status_Transitions](https://codex.wordpress.org/Post_Status_Transitions)
   [https://codex.wordpress.org/Function_Reference/wp_transition_post_status](https://codex.wordpress.org/Function_Reference/wp_transition_post_status)
 *  Thread Starter [Lzhelenin](https://wordpress.org/support/users/lzhelenin/)
 * (@lzhelenin)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/using-wp_query-in-functionphp/#post-6057583)
 * Thanks Navneil, but can’t find solution in the articles… tried to do it with ‘
   transition_post_status’, but everything is the same
 *     ```
       <?php
       add_action('transition_post_status', function($new_status, $old_status, $post)
       {
   
           if ('publish' == $new_status && $post->post_type == 'opt') {
   
               $args      = array(
                   'post_type' => 'opt'
               );
               $opt_array = array();
               $the_query = new WP_Query($args);
               sleep(3);
               while ($the_query->have_posts()):
                   $the_query->the_post();
                   $good_type_array = array();
                   while (have_rows('good_type')):
                       the_row();
                       $good_type_name = get_sub_field('good_type_name');
                       array_push($good_type_array, $good_type_name);
                   endwhile;
                   array_push($opt_array, array(
                       'name' => get_the_title(),
                       'good_type' => $good_type_array,
                       'price' => get_field('price')
                   ));
               endwhile;
               wp_reset_postdata();
               file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/blablabla/json/data.json', raw_json_encode($opt_array));
           }
       }, 10, 3);
       ?>
       ```
   
 *  Thread Starter [Lzhelenin](https://wordpress.org/support/users/lzhelenin/)
 * (@lzhelenin)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/using-wp_query-in-functionphp/#post-6057585)
 * What I found out is that the function DOES write the file. But only if I:
 * 1. Change the title of the custom post
 * Or
 * 2. Press ‘update’ button two times. For example, if I change number in a custom
   field to ‘123’ and press the ‘update’ button, then change it to ‘1234’ and press
   the button again, after the second time it’ll write ‘123’ value in the file.
 *  Thread Starter [Lzhelenin](https://wordpress.org/support/users/lzhelenin/)
 * (@lzhelenin)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/using-wp_query-in-functionphp/#post-6057704)
 * Tried to do it with general wordpress posts and simplier code… still writes the
   file only after I press the ‘update’ button the second time
 *     ```
       function opt_update($ID, $post) {
           $args = array(
               'post_type' => 'post'
           );
           $opt_array = array();
           $the_query = new WP_Query($args);
           while ($the_query -> have_posts()): $the_query -> the_post();
           array_push($opt_array, array(
               'price' => get_field('price')
           ));
           endwhile;
           wp_reset_postdata();
           file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/blablabla/json/data.json', raw_json_encode($opt_array));
       }
       add_action('publish_post', 'opt_update', 10, 2);
       ```
   

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Using WP_query() in function.php’ is closed to new replies.

## Tags

 * [custom_post_type](https://wordpress.org/support/topic-tag/custom_post_type/)
 * [hook](https://wordpress.org/support/topic-tag/hook/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 4 replies
 * 2 participants
 * Last reply from: [Lzhelenin](https://wordpress.org/support/users/lzhelenin/)
 * Last activity: [11 years, 1 month ago](https://wordpress.org/support/topic/using-wp_query-in-functionphp/#post-6057704)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
