Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My custom query works, except when debugging it throws off an error:

Undefined Offset: 0 in /wp-includes/query.php

This is what my query looks like :

<?php $loop = new wp_query('post_type=car&category_name='.get_the_title());?>

<?php if( $loop->have_posts() ) : ?>
    <table style="width: 100%">
        <col style="width:35%">
        <col style="width:30%">
        <col style="width:20%">
        <col style="width:15%">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Style</th>
                    <th>ABC</th>
                    <th>Rating</th>
                </tr>
            </thead>
            <tbody>
                <?php while( $loop->have_posts() ) : $loop->the_post(); ?>
                <tr>
                    <td><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></td>
                    <td><?php $style = get_post_meta( get_the_ID(), 'style', true); echo $style; ?></td>
                    <td><?php $abc = get_post_meta( get_the_ID(), 'abc', true); echo $abc; ?></td>
                    <td><?php $rating = get_post_meta( get_the_ID(), 'rating', true); echo $rating; ?></td>

                </tr>
                <?php endwhile; else: endif; ?>
                </tbody>
                </table>

I've looked up Undefined Offset: 0, but I can't seem to figure out what I'm missing here. What could be wrong?

share|improve this question
    
You are passing an invalid value to category_name. category_name accepts the **slug** of the category. You want to pass a post name to it. This is a bit off topic, but your closing tbody` and table tags are place in the wrong place. tbody should be before else and table before endif. Also, please indent your code properly before adding it to the question. It is hard to read –  Pieter Goosen Jul 28 at 4:30
    
Hi Pieter, thanks for your response. I probably should have explained my query a bit better. I'm trying to pull the posts listed under the custom post type "car" that are in the category named the same as the page this loop is in. Hopefully that makes sense. Thank you for the </tbody> and </table> note! –  ThisGuyPhill Jul 28 at 4:45
    
But the slug and title is different, means your title may contain space, but that is replaced by - in slug.so try to use slug –  Bindiya Patoliya Jul 28 at 5:05
    
I'm trying to avoid using a slug because multiple posts that contain this table will use the same title, will each of these posts have a different slug? Ex. post-slug and post-slug-1 for two posts with the title "Post Slug". Using the title seemed to be my best option. –  ThisGuyPhill Jul 28 at 5:20

2 Answers 2

Try like this :

$args = array( 
    'post_type' => 'car',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'your category/taxonomy name for post type car',
            'terms' => 'term name/category name',
            'field' => 'slug'
        )
    )
);
$query = new WP_Query($args);
$results = $query->get_posts();
print_r($results);

Yes every post has different slug, even if you have given same name. For this you can create taxonomy term like which you have given post name, and assign this term to post you have in this category/term.

Ex.

Create taxonomy term named Post Slug and assign it to both post and try my query with your respective parameters.

share|improve this answer
    
Thanks Bindiya. I think I'm going to try a different route. Since this table is going into a custom post type I've enabled that custom post type to use regular post categories. Both the post this table is going into and the posts that will be entered into the table now use wordpress standard categories. I'm trying to use <?php $catid = get_cat_ID($post->ID); ?> and <?php $cars = array( 'post_type' => 'car', 'cat' => '$catid' ); ?> to pull posts in the same category as the post the table is placed within. I still get the same error though. –  ThisGuyPhill Jul 28 at 6:05
    
You can not assign custom post type to default category.you have to create custom taxonomy/category –  Bindiya Patoliya Jul 28 at 6:11
    
This doesn't allow a custom post type to use the standard categories? register_taxonomy_for_object_type( 'category', '$custompost' ); –  ThisGuyPhill Jul 28 at 6:15
    
Yes you can try this way. –  Bindiya Patoliya Jul 28 at 6:31
up vote 1 down vote accepted

UPDATE Here is my final code. It works perfectly.

<?php $pageTitle = get_the_title();
// Query Arguments
 $args = array (
    'post_type'              => 'car',
    'order'                  => 'ASC',
    'meta_query'             => array(
               array(
                  'key'       => 'brand_category',
                  'value'     => $pageTitle
        ),
    ),
 );

 // The Query
  $query = new WP_Query( $args ); 

// The Loop
 if ( $query->have_posts() ) {
  echo '<h3>Cars</h3>
    <table style="width: 100%">
       <col style="width:35%">
       <col style="width:30%">
       <col style="width:20%">
       <col style="width:15%">
     <thead>
       <tr>
         <th>Name</th>
         <th>Style</th>
         <th>ABC</th>
         <th>Rating</th>
       </tr>
    </thead>
 <tbody>';
while ( $query->have_posts() ) {
    $query->the_post();
    echo '<tr>';
       echo '<td>';
       echo '<a href="'.get_permalink().'">'.get_the_title().'</a>';
       echo '</td>';
       echo '<td>' . get_post_meta( get_the_ID(), 'style', true) . '</td>
       <td>' . get_post_meta( get_the_ID(), 'abc', true) . '</td>
       <td>' . get_post_meta( get_the_ID(), 'rating', true). '</td>                                    
    </tr>';
   }
echo '</tbody>
</table>';
} else { 
   // no posts found
}

// Restore original Post Data
wp_reset_postdata(); 
?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.