Title: What Is Wrong With My Code? (PHP &amp; JS)
Last modified: August 22, 2023

---

# What Is Wrong With My Code? (PHP & JS)

 *  [lukeyk94](https://wordpress.org/support/users/lukeyk94/)
 * (@lukeyk94)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/what-is-wrong-with-my-code-php-js/)
 * I’m using PHP & JS code snippets to find the meta field values of posts that 
   are displayed from the content views plugin. The JS works but every post says“
   undefined”. I used ChatGPT to help with the code.
 *     ```wp-block-code
       <?php
       // Get the "time" meta value for each post displayed by the Content Views plugin
       $times = array();
       $posts = get_posts(array('post_type' => 'post', 'posts_per_page' => -1));
       foreach ($posts as $post) {
           $time = get_post_meta($post->ID, 'time', true);
           $times[$post->ID] = $time;
       }
       ?>
   
       <script>
       jQuery(document).ready(function($) {
           // Parse the "times" string as a JSON object
           var times = JSON.parse('<?php echo json_encode($times); ?>');
   
           // Append the "time" meta value to each post
           $('.pt-cv-content-item').each(function() {
               var postId = $(this).find('a').attr('href').split('/').pop();
       ```
   

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

 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/what-is-wrong-with-my-code-php-js/#post-16993367)
 * I don’t see right now in what context you are running the code. You talk about
   a “content views plugin” – what do you mean by that?
 * By the way, using ChatGPT as a starting point is NOT a good idea. The code you
   show is incomplete and buggy.
 *  Thread Starter [lukeyk94](https://wordpress.org/support/users/lukeyk94/)
 * (@lukeyk94)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/what-is-wrong-with-my-code-php-js/#post-16993636)
 * [@threadi](https://wordpress.org/support/users/threadi/) I’m using WPCode (plugin)
   to manually add code snippets to pages on my site. In this case, I’m trying to
   modify the output of a plugin that displays posts in a grid- that plugin is called
   Content Views. I managed to get JavaScript to append the meta field to each post
   in the plugin, but I think I’m running into an issue getting the post meta data
   field using PHP.
    -  This reply was modified 2 years, 9 months ago by [lukeyk94](https://wordpress.org/support/users/lukeyk94/).
 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/what-is-wrong-with-my-code-php-js/#post-16994112)
 * Okay, your goal is clear. But in what context do you execute the code? It should
   actually be based on a hook if you want to add something somewhere.
 * It might make more sense if you contact the Content Views support: [https://wordpress.org/support/plugin/content-views-query-and-display-post-page/](https://wordpress.org/support/plugin/content-views-query-and-display-post-page/)

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

The topic ‘What Is Wrong With My Code? (PHP & JS)’ is closed to new replies.

## Tags

 * [javascript](https://wordpress.org/support/topic-tag/javascript/)
 * [jquery](https://wordpress.org/support/topic-tag/jquery/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 2 participants
 * Last reply from: [threadi](https://wordpress.org/support/users/threadi/)
 * Last activity: [2 years, 9 months ago](https://wordpress.org/support/topic/what-is-wrong-with-my-code-php-js/#post-16994112)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
