我有两个WordPress安装程序,一个用作内部网,另一个是公共的。我需要从公共站点中提取自定义post类型的数据,以便在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和title,但由于ACF数据存储在不同的表中,我不确定如何a)访问该数据和b)将其链接到正确的post。

最佳答案

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

10-06 03:10