• 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.

    <?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

    (@threadi)

    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

    (@lukeyk94)

    @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.
    Moderator threadi

    (@threadi)

    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://ww.wp.xz.cn/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.