Support

Account

Home Forums Gutenberg Get the ACF Gutenberg Block Value in functions.php

Unread

Get the ACF Gutenberg Block Value in functions.php

  • Is it possible to get the values of ACF Gutenberg Blocks outside the block in functions.php?

    I need to get the values in functions.php and send it to “wp_localize_script” to grab the value in jQuery and render the value in WordPress admin Dashboard via “admin_enqueue_scripts” hook where the User will select the post.

    And then, after getting the success value on Ajax call, need to render the view of selected posts or posts?

    N B: When I tried to local with ACF pro somehow the ACF Gutenberg Block value shows in functions.php but on the live server it isn’t coming.

    on functions.php:

    
    <?php
    function workwear_admin_assets()
    {
        $ids = [];
        $data = $sdata = '';
        $howtoDisplay = get_field('how_you_want_to_display');
        $postsCategory = get_field('posts_by_catgory');
        $select_posts = get_field('select_posts');
    
        if ($howtoDisplay[0] == 'recent' && $howtoDisplay != '') {
            $data = new WP_Query(array('post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 9));
        }
        if ($howtoDisplay[0] == 'category' && $postsCategory) {
            $data = new WP_Query(array('post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 9, 'cat' => $postsCategory));
        }
        if ($howtoDisplay[0] == 'custom' && $select_posts) {
            $sdata = $select_posts;
        }
        if ($data) :
            while ($data->have_posts()) : $data->the_post();
                $ids[] = array(
                    'id' => get_the_ID(),
                );
    
            endwhile;
        endif;
    
        if ($sdata) {
            foreach($sdata as $select){
                $ids[] = array(
                    'id' => $select->ID,
                );
            }
    
        }
        wp_reset_postdata();
        wp_enqueue_script('admin-js', get_template_directory_uri() . "/js/admin.js", array('jquery'), rand(111, 999), true);
        wp_localize_script('admin-js', 'admin_posts', array(
            'ajaxurl' => admin_url('admin-ajax.php'),
            'ids' => $ids,
        ));
    }
    add_action('admin_enqueue_scripts', 'workwear_admin_assets');
    

    on admin.js:

    ;(function($){
           //admin posts load
    
        $(document).ready(function(){
            data ={
                "action" : "posts_admin",
                "id" : admin_posts.ids,
                // "id" : $('.custom-data').attr('data-id'), 
            };
            console.log(data);
            $.ajax({
                url: admin_posts.ajaxurl,
                data: data,
                type: 'POST',
                beforeSend: function () {
                    $(this).text('Loading...'); // change the button text, you can also add a preloader image
                },
                success: function(data){
                    $('.render-posts').html(data).fadeIn('slow');
                    console.log(data);
                }
            });
        });
    })(jQuery);
    

    Ajax Calling:

    
    <?php
    function workwear_admin_posts()
    {
        $sid = [];
        $blog_ids = $_POST['id'];
        foreach ($blog_ids as $blog_id) {
            $sid[] .= $blog_id['id'];
        }
    
        $args = [
            'post_type' => 'post',
            'post_status' => 'publish',
            'posts_per_page' => 9,
            'post__in' => $sid,
        ];
        var_dump($args);
        $blog = new WP_Query($args);
        echo '<div class="row">';
        if ($blog->have_posts()) :
            while ($blog->have_posts()) : $blog->the_post();
            ?>
    
                <div class="col-md-4">
                    <div class="blog">
                        <?php echo '<img src="' . get_the_post_thumbnail_url(get_the_ID()) . '" widht="376px" height="282px" alt="Article Image">';
                        echo '<h4><a href="' . get_the_permalink(get_the_ID()) . '">' . get_the_title() . '</a></h4>'; ?>
                    </div>
                </div>
    <?php
            endwhile;
        endif;
        echo '</div>';
        wp_reset_postdata();
        die();
    }
    add_action('wp_ajax_posts_admin', 'workwear_admin_posts');
    

    ACF Meta Field
    WordPress Dashboard Post Render

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.