WordPress function to get the value of a specific field

Lately, I’ve been assigned with the task of removing the ACF plugin from our instance and reimplementing its functions using WordPress functions. The thing is, we’ve used some plugin functions to customize our theme. Here is a snippet:

<?php
                                $terms = get_sub_field(‘simuladores_em_destaque’);

                                if ($terms) : ?>
                                    <?php foreach ($terms as $term) : ?>
                                        <?php

                                        $args = array(
                                            ‘post_type’ => ‘page’,
                                            ‘tax_query’ => array(
                                                array(
                                                    ‘taxonomy’ => ‘simuladores_esb’,
                                                    ‘field’ => ‘slug’,
                                                    ‘terms’ => $term->slug,
                                                ),
                                            ),
                                        );
                                        $query = new WP_Query($args);
                                        if ($query->have_posts()) {
                                            $query->the_post();
                                        ?>
                                            <div class=”col text-center”>
                                                <a href=”<?= get_permalink($query->post->ID) ?>” onclick=”registraEvento(‘Destaque Simuladores’,’Clique’,'<?= $term->name ?>’);”>
                                                    <img width=”100″ height=”100″ src=”<?= the_field(‘icone’, $term) ?>” alt=”<?= $query->the_title() ?>” />
                                                    <div class=”texto-simuladores”>
                                                        <?= the_field(‘label’, $term) ?>
                                                    </div>

Right now, I am struggling with the\_field() function, I tried to replace it with the get\_post\_meta function, however I do not have a post to pass it as argument to the function, all I have is a WP\_Term object. The ACF plugin is, probably, able to extract the post id from the Term obejct. Does anybody know this is done? Or have any suggestions on how to tackle this issue?

6 Comments
  1. Why do you want to remove ACF? I’ve build hundreds of sites and it’s used on literally every single one.

  2. Honestly… repeaters are going to give you a hell of a time to pull out and replicate, which is what get_sub_field() is used for.

    Take a look at the meta_table and how those are saved. If you don’t use the plugin, I would just keep it around for the legacy code and then for anything new just use the WordPress functions.

  3. You need to reorganize your code and add what’s below.

    // Get ID using existing WordPress function
    $post_id = get_the_ID();

    // Get terms using get_post_meta() function. ACF saves data as post meta.
    $terms = get_post_meta($post_id, ‘simuladores_em_destaque’, true);

    // Now grab the specific data you are looking for to fill your fields.
    $icone_url = get_post_meta(get_the_ID(), ‘icone’, true);
    $label = get_post_meta(get_the_ID(), ‘label’, true);

    If you remove ACF you won’t be able to easily edit the data.

    I don’t see any reason to remove ACF. It’s one of the most used plugins, the free version is what you want.

    When you inject make sure you escape.
    [https://developer.wordpress.org/apis/security/escaping/](https://developer.wordpress.org/apis/security/escaping/)

  4. Honestly, if you are considering “reimplementing” (retrofitting is a more accurate term IMO) then you have more than just output to worry about. You also will lose the ability to update the fields. It creates a situation where frontend is using data that the poor soul coming into the project 6 months down the road will have no way to reference where it came from…even if that poor soul is you. How do you plan to reconcile that? Hmmm?

    I would recommend thinking through the entire system. Can you alter it to use free ACF? What about Carbon Fields? Those are both just abstractions, you could also roll your own custom fields, it’s not that hard. The point is, you likely have to migrate the data to a different structure anyway so this entire thread will become a moot point. Figure your data out and *then* worry about how to query it.

  5. Unless your hourly rate is extremely low, it’s going to take longer to rebuild using base WP functions than the hourly rate conversion for purchasing an ACF Pro license for $49/yr.

    Rebuilding with static elements is definitely viable, but it still will probably take a few more hours than the Pro license cost equivalent.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer