php - 如何使用 'wpdb' 访问自定义帖子类型的 ACF 字段?

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

我有两个 WordPress 安装,一个用作内部网,另一个是公共(public)的。我需要从公共(public)站点提取自定义帖子类型的数据以显示在 Intranet 上,基本上这样管理员就不需要在每个站点上输入相同的数据。

到目前为止,我得到了这个:

$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>';

}

这按预期引入了 slug 和标题,但由于 ACF 数据存储在不同的表中,我不确定如何 a) 访问该数据以及 b) 将其链接到正确的帖子。

最佳答案

获取自定义帖子类型的 ACF 字段

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

}
?>

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

关于php - 如何使用 'wpdb' 访问自定义帖子类型的 ACF 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44564169/

相关文章:

mysql - 将 SQL 查询转换为 Codeigniter Active Record

javascript - jQuery 函数仅在第一行运行

php - 使用 php 一次一页地显示数据

php - 开始使用 YouTube 分析 API?

php - 如何在 PHP 中逐行写入文本文件?

php - DB::select 过程从输入请求返回空数组

mysql - RDS MySQL 错误 1045 (28000) : Access denied for user when connecting to db from EC2

mysql - 是否有将多对多链接表连接到单个记录的查询?

mysql - 如何为 Buddypress 配置 Amazon EC2 t1.micro 实例

javascript - 为什么将注释的开头向下移动一行空格会破坏此 JavaScript 代码在 WordPress 中的编译?