从Wordpress输出查询到CSV

时间:2015-09-04 21:10:30

标签: php wordpress csv

我创建了一个查询,它从数据库中提取了我想要的值,并将它们放入一个数组中(var_dump显示了这个)但是没有创建文件。这是从一个大杂烩的源头拼凑到一起,以达到这个目的。某处我错过了实际创建CSV文件并强制下载的内容。

function wpcp_export_inventory_feed()
{


$args = array(
    'showposts'        => -1,
'offset'           => 0,
'orderby'          => 'date',
'order'            => 'DESC',
'post_type'        => 'inventory',
'post_status'      => 'publish',
'suppress_filters' => true 
);

$posts = get_posts($args);

$fh = fopen( 'wp-content/uploads/export.csv', 'w');

$vehicles[] = array();
$vehicles[] = array('Stock', 'Year', 'Make', 'Model', 'Trim', 'Auto Title', 'VIN', 'Price', 'Discount', 'MSRP', 'Mileage', 'Body Style', 'Engine', 'Transmission', 'Exterior Color', 'Interior Color', 'Fuel', 'MPG City', 'MPG Hwy');

while ( have_posts() ) : the_post();

    $autoID = get_the_ID();
    $year = get_post_meta( $autoID, '_auto_year', true );
    $make = get_post_meta( $autoID, '_auto_make', true );
    $model = get_post_meta( $autoID, '_auto_model', true );
    $trim = get_post_meta( $autoID, '_auto_trim', true );
    $autotitle = "$year $make $model $trim" ;
    $autostock = get_post_meta( $autoID, '_auto_stock', true );
    $autovin = get_post_meta( $autoID, '_auto_vin', true );
    $autoprice = get_post_meta( $autoID, '_auto_price', true );
    $autodiscount = get_post_meta( $autoID, '_auto_discount', true );
    $automsrp = get_post_meta( $autoID, '_auto_msrp', true );
    $automileage = get_post_meta( $autoID, '_auto_mileage', true );
    $autobodystyle = get_post_meta( $autoID, '_auto_body', true );
    $autoengine = get_post_meta( $autoID, '_auto_engine', true );
    $autotrans = get_post_meta( $autoID, '_auto_trans', true );
    $autocolorext = get_post_meta( $autoID, '_auto_color_ext', true );
    $autocolorint = get_post_meta( $autoID, '_auto_color_int', true );
    $autofuel = get_post_meta( $autoID, '_auto_fuel', true );
    $autompgcity = get_post_meta( $autoID, '_auto_gasmiles_city', true );
    $autompghwy = get_post_meta( $autoID, '_auto_gasmiles_hwy', true );

    $vehicles[] = array($autostock, $year, $make, $model, $trim, $autotitle, $autovin, $autoprice, $autodiscount, $automsrp, $automileage, $autobodystyle, $autoengine, $autotrans, $autocolorext, $autocolorint, $autofuel, $autompgcity, $autompghwy);

endwhile;

foreach($vehicles AS $vehicle) { fputcsv($fh, $vehicle, ';'); }
fclose($fh);

var_dump($vehicles);

}

1 个答案:

答案 0 :(得分:0)

我认为你有路径问题。如果逻辑的其余部分出现问题,至少你会有一个零大小的文件。如果您扫描所有文件夹,您可能会找到它。

您需要为路径提供根锚:

$fh = fopen ('/wp-content/uploads/export.csv', 'w');