【问题标题】:How to access ACF fields for a custom post type using 'wpdb'?如何使用“wpdb”访问自定义帖子类型的 ACF 字段?
【发布时间】:2017-11-17 18:01:10
【问题描述】:

我有两个 WordPress 安装,一个用作 Intranet,另一个是公共的。我需要从公共站点提取自定义帖子类型的数据,以便在 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) 将其链接到正确的帖子。

【问题讨论】:

  • 在第二个参数中添加帖子ID:get_field('career_duration', $career->ID)
  • 思考了几分钟,我得出的结论是,这可能就是这么简单,但是尝试了它,它不起作用,没有任何返回。

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


【解决方案1】:

获取自定义帖子类型的 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);
?>

【讨论】:

    【解决方案2】:

    Shital,我看不出您的第二个解决方案没有引用外部数据库,它会如何工作?

    我尝试了以下方法(类似于您的第一个解决方案),但它也没有为 ACF 字段返回任何内容:

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

    【讨论】:

    • 是的,根据我的第一篇文章,所有非 ACF 数据都正确返回,以及新添加的 ID 变量。但是,将 ID 应用于 ACF 调用的第二个参数不会返回任何内容。
    • 打开错误,我现在看到:“WordPress 数据库错误:[您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本对应的手册,以了解在 ' 附近使用的正确语法WHERE ID = 5414 LIMIT 1' at line 1] SELECT * FROM WHERE ID = 5414 LIMIT 1"
    • 修复了该错误,是语法问题。因此,与未显示的数据无关。
    • $wpdb->get_results(" SELECT ID, post_date, post_title, post_content, meta_key, meta_value FROM {$wpdb->posts} INNER JOIN {$wpdb->postmeta} ON ($wpdb-> post.ID = {$wpdb->postmeta}.post_id ) " );
    猜你喜欢
    • 2019-10-03
    • 2021-01-09
    • 2020-06-11
    • 2019-06-27
    • 2019-06-12
    • 2019-11-14
    • 2021-07-22
    • 1970-01-01
    • 2016-04-27
    相关资源
    最近更新 更多