How to access ACF fields for a custom post type using 'wpdb'?

时间:2017-06-15 09:42:15

标签: php mysql wordpress custom-post-type advanced-custom-fields

I have two WordPress 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) {

            echo '<li class="career">';

                echo '<a class="wrap" href="http://domainname.com/career/'.$career->post_name.'" target="_blank">';

                    echo '<h2>'.$career->post_title.'</h2>';
                    echo get_field('career_duration') ? '<p class="duration">('.get_field('career_duration').')</p>' : '<p class="duration">(permanent)</p>';
                    echo '<h3>'.get_field('career_area').'</h3>';
                    echo '<p class="description">'.get_field('career_short_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 as the ACF data is stored in a different table, I am not sure how to a) access that data and b) link it to the correct post.

2 个答案:

答案 0 :(得分:1)

Fetch ACF fields for a custom post type

<?php
$wpdb = new wpdb(*CONNECTION DETAILS*);
$careers = $wpdb->get_results(
    'SELECT *
    FROM  wp_posts
    WHERE post_type = "career-post"'
);

if (!empty($careers)) {
    ?>
    <ul id="careers-list">
        <?php
         foreach ($careers as $career) {

            $career_duration = get_field('career_duration', $career->ID);
            $career_area = get_field('career_area', $career->ID);
            $career_short_description = get_field('career_short_description', $career->ID);
        ?>
            <li class="career">
                <a href="<?php echo get_permalink($career->ID); ?>" target="_blank">
                    <h2><?php echo get_the_title( $career->ID ); ?></h2>
                    <?php
                    if($career_duration!="")
                    {
                    ?> 
                    <p class="duration"><?php echo $career_duration;?></p> 
                    <?php   
                    }
                    else
                    {
                    ?> 
                    <p class="duration">permanent</p> 
                    <?php       
                    }
                    ?>
                    <h3><?php echo $career_area;?></h3>
                    <p class="description"><?php echo $career_short_description;?></p>
                </a>
            </li>
            <?php
         }
        ?>
         </ul>
    <?php
}
else {

    echo '<h2>Sorry, no vacancies are currently listed.</h2>';

}
?>

OR

<?php
$career_post_type = 'career-post';
$career_post_args=array(
    'type'                     => $career_post_type,
    'post_status'              => 'publish',
    'posts_per_page'           => -1,
    'caller_get_posts'         => -1,
    'orderby'                  => 'name',
    'order'                    => 'ASC',
);
$career_post_my_query = null;
$career_post_my_query = new WP_Query($career_post_args);


if( $career_post_my_query->have_posts() ) 
{
    ?>
    <ul id="careers-list">
        <?php
        while ($career_post_my_query->have_posts()) : $career_post_my_query->the_post(); 

            $career_duration = get_field('career_duration', $post->ID);
            $career_area = get_field('career_area', $post->ID);
            $career_short_description = get_field('career_short_description', $post->ID);
        ?>
            <li class="career">
                <a href="<?php echo get_permalink($post->ID); ?>" target="_blank">
                    <h2><?php echo get_the_title( $post->ID ); ?></h2>
                    <?php
                    if($career_duration!="")
                    {
                    ?> 
                    <p class="duration"><?php echo $career_duration;?></p> 
                    <?php   
                    }
                    else
                    {
                    ?> 
                    <p class="duration">permanent</p> 
                    <?php       
                    }
                    ?>
                    <h3><?php echo $career_area;?></h3>
                    <p class="description"><?php echo $career_short_description;?></p>
                </a>
            </li>
            <?php
           endwhile;
        ?>
         </ul>
    <?php
}
else {

    echo '<h2>Sorry, no vacancies are currently listed.</h2>';

}
wp_reset_query($career_post_my_query);
?>

答案 1 :(得分:0)

Shital, I don't see how your second solution would work as it's not referencing the external DB?

I've tried the following (similar to your first solution) and it doesn't return anything for the ACF fields either:

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="http://domainname.com/career/'.$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>';

}