gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:44:41 25 4
gpt4 key购买 nike

我有两个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com