Home Forums General Issues How to access ACF data for posts using 'wpdb'?


How to access ACF data for posts using 'wpdb'?

  • I have two WP installs, one is to be used as an intranet and the other is public. I need to pull in the data for a custom post type from the public site to be shown on the intranet, essentially so the admin doesn’t need to enter the same data on each site.

    So, far I’ve got this:

    $wpdb = new wpdb(*CONNECTION DETAILS*);
    $careers = $wpdb->get_results(
        'SELECT *
        FROM  wp_posts
        WHERE post_type = "career-post"'
    if (!empty($careers)) {
        echo '<ul id="careers-list">';
    		foreach ($careers as $career) {
    		    $ID = $career->ID;
    		    $slug = $career->post_name;
    		    $title = $career->post_title;
    		    $duration = get_field('career_duration', $ID);
    		    $area = get_field('career_area', $ID);
    		    $description = get_field('career_short_description', $ID);
    		    echo '<li class="career">';
    		        echo '<a class="wrap" href="'.$slug.'" target="_blank">';
    		            echo '<h2>'.$title.'</h2>';
    		            echo $duration ? '<p class="duration">('.$duration.')</p>' : '<p class="duration">(permanent)</p>';
    		            echo '<h3>'.$area.'</h3>';
    		            echo '<p class="description">'.$description.'</p>';
    		        echo '</a>';
    		    echo '</li>';
        echo '</ul>';
    else {
        echo '<h2>Sorry, no vacancies are currently listed.</h2>';

    Which is pulling in the slug and title as expected, but not the ACF data, as presumably it’s stored in a different table. I am not sure how to a) access that data and b) return it against each post.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.