如何循环访问自定义帖子并将标题添加到数组

时间:2019-09-14 作者:Dan Sutherland

我正在尝试查询一些自定义帖子,以便可以对它们进行迭代,提取ecah的标题并将标题添加到数组中。我这样做的原因是,我可以使用数组内容在woocommerce中创建一个选择字段来生成选项(尚未完成此操作)

有没有人能看到我在这段代码中犯了什么错误,或者提供了什么建议,我无法让它发挥作用。网站宕机了,我在var\\u垃圾堆里什么也没得到

add_action(\'woocommerce_after_order_notes\', \'custom_checkout_field\');

function custom_checkout_field($checkout)

{

$args = array(
    \'post_type\'         => \'pickup-point\',
    \'posts_per_page\'    => -1,
    \'post_status\'       => \'publish\'
    );

// Custom query to pull in the pickup points.
$query = new WP_Query( $args );

// Check that we have query results.
if ( $query->have_posts() ) {


    $available_locations = [];

  while ( $query->have_posts() ) {

        $query->the_post();

        array_push($available_locations, the_title());

    }
}

var_dump ($available_locations);

}

// Restore original post data.
wp_reset_postdata();

3 个回复
SO网友:Antti Koskinen

我发现您编写的代码有两个问题。第一个是the_title() 重复文章标题,但在这种情况下,返回标题更合适。而且wp_reset_postdata() 应在while 循环内部if 陈述

但正如您所提到的,您只需要帖子标题,就可以直接从查询对象的帖子对象中获取它们,而无需设置循环have_posts()the_post(). 就像这样,

add_action(\'woocommerce_after_order_notes\', \'custom_checkout_field\');
function custom_checkout_field($checkout) {

  $args = array(
    \'post_type\'              => \'pickup-point\',
    \'posts_per_page\'         => -1,
    \'post_status\'            => \'publish\',
    \'no_found_rows\'          => true, // pagination not needed
    \'update_post_meta_cache\' => false, // post meta not needed
    \'update_post_term_cache\' => false, // taxonomy terms not needed
  );
  $query = new WP_Query( $args );

  $titles = array_map( \'get_the_title\', $query->posts );
  if ( $titles ) {
    // do something with $titles    
  }

}
我在查询中添加了几个额外的参数$args, 这使得查询更加高效,因为只需要帖子标题。

SO网友:LoicTheAztec

主要问题是您需要使用get_the_title() 而不是the_title() 在代码中,作为the_title() 已回显,无法设置为变量。

您似乎希望在WooCommerce结账页面的下拉(或单选按钮)字段中显示您的取货位置(自定义帖子类型)。

您也可以简单地使用get_posts() 功能如下:

add_action(\'woocommerce_after_order_notes\', \'custom_checkout_field\');

function custom_checkout_field( $checkout ) {

    $pickup_points = (array) get_posts( array(
        \'post_type\'         => \'pickup-point\',
        \'posts_per_page\'    => -1,
        \'post_status\'       => \'publish\'
    ) );

    if ( count( $pickup_points ) > 0 ) {

        $available_locations = []; // Initializing

        // Loop though each \'pickup-point\' WP_Post object
        foreach ( $pickup_points as $pickup_point ) {
            // Set current post title in the array
            $available_locations[] = $pickup_point->post_title;
        }

        // testing output
        var_dump ($available_locations);
    }
}
或者如果你有WP_Query:

add_action(\'woocommerce_after_order_notes\', \'custom_checkout_field\');

function custom_checkout_field( $checkout ) {

    // Custom query to pull in the pickup points.
    $query = new WP_Query( array(
        \'post_type\'         => \'pickup-point\',
        \'posts_per_page\'    => -1,
        \'post_status\'       => \'publish\'
    ) );

    $available_locations = []; // Initializing

    // Check that we have query results.
    if ( $query->have_posts() ) :

    $available_locations = []; // Initializing

    while ( $query->have_posts() ) : $query->the_post();

    $available_locations[] = get_the_title(); // Set the current title in the array

    endwhile;

    // testing output
    var_dump ($available_locations);

    wp_reset_postdata();

    endif;
}
两者应该以相同的方式工作。

SO网友:Jacob Peattie

如果您使用get_posts():

$posts = get_posts(
    array(
        \'post_type\'         => \'pickup-point\',
        \'posts_per_page\'    => -1,
        \'post_status\'       => \'publish\'
    )
);

$titles = array_map( \'get_the_title\', $posts );
这充分利用了您可以通过WP_Post 反对get_the_title() 通过使用array_map() 要运行get_the_title() 关于get_posts(), 这是一个数组WP_Post 对象。

相关推荐