0
votes

I'm using this piece of code to pull out cars and the car details in a custom post type within wordpress.

<h1>Cars for Sale</h1>
<?php $args = array( 'post_type' => 'carlistings', 'posts_per_page' => 10 ); ?>
<ul id="carsforsale">
    <?php $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <?php $cardetails = get_post_meta( $post->ID, 'cardetails', true );
            foreach( $cardetails as $cardetails ) { ?> <!-- Line # 13 -->
        <li class="pop"><span>
            <span class="thumb"><a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('thumbnail'); } ?></a></span>
            <span class="title"><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></span>
            <strong>Make</strong> <?php echo $cardetails['make'] ?><br />
            <strong>Model</strong> <?php echo $cardetails['model'] ?><br />
            <strong>Year</strong> <?php echo $cardetails['year'] ?><br />
            <strong>Color:</strong> <?php echo $cardetails['color'] ?><br />
            <strong>Price:</strong> <?php echo $cardetails['price'] ?><br />
        </span></li>
<?php } ?>
<?php wp_reset_query(); ?>

The problem I have is, that after 3 enteries I get this error message

Warning: Invalid argument supplied for foreach() in /home/styleaut/public_html/wp-content/themes/styleautosales/page-30.php on line 13

line 13 falls on this part of the code:

foreach( $cardetails as $cardetails ) {
3
this means that $cardetails is not an array or objectphp_nub_qq
Could you please advise further? My PHP knowledge is awful!Brad Fletcher
please print your variable using print_r($cardetails) and paste your result hereKhushboo
Array ( [price] => £16,995 [year] => 2009 [make] => Ford [model] => Mustang GT Premium [color] => Vapor Silver [location] => UK )Brad Fletcher

3 Answers

1
votes

Add if statement before foreach like this:

<h1>Cars for Sale</h1>
<?php $args = array( 'post_type' => 'carlistings', 'posts_per_page' => 10 ); ?>
<ul id="carsforsale">
    <?php $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <?php $cardetails = get_post_meta( $post->ID, 'cardetails', true );
            if($cardetails) {
            foreach( $cardetails as $cardetails ) { ?>
        <li class="pop"><span>
            <span class="thumb"><a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail('thumbnail'); } ?></a></span>
            <span class="title"><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></span>
            <strong>Make</strong> <?php echo $cardetails['make'] ?><br />
            <strong>Model</strong> <?php echo $cardetails['model'] ?><br />
            <strong>Year</strong> <?php echo $cardetails['year'] ?><br />
            <strong>Color:</strong> <?php echo $cardetails['color'] ?><br />
            <strong>Price:</strong> <?php echo $cardetails['price'] ?><br />
        </span></li>
<?php } // end foreach
} // end if ?>
<?php wp_reset_query(); ?>
0
votes

I think your problem is the way you're calling get_post_meta. By setting the last parameter to true, it will return the value in the first row of the post meta, which will always be a string. Try calling the function without that parameter.

-1
votes

Change

foreach( $cardetails as $cardetails ) {

to

foreach( $cardetails as $value ) {

and you will need to change all $cardetails variables usage in the body of foreach to $value, f.e.:

$cardetails['make']

needs to be changed to:

$value['make']

$cardetails['model'] to $value['model']

and so on..