web-dev-qa-db-fra.com

La requête personnalisée fonctionne mais renvoie "Décalage non défini: 0"

Ma requête personnalisée fonctionne, sauf lorsque le débogage génère une erreur:

Décalage non défini: 0 dans /wp-includes/query.php

Voici à quoi ressemble ma requête:

<?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>

J'ai regardé Décalage non défini: 0, mais je n'arrive pas à comprendre ce qui me manque ici. Quel pourrait être le problème?

1
ThisGuyPhill

UPDATE Voici mon code final. Ça fonctionne parfaitement.

<?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(); 
?>
1
ThisGuyPhill

Essayez comme ça:

$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);

Oui, chaque publication a un slug différent, même si vous avez donné le même nom. Pour cela, vous pouvez créer un terme de taxonomie comme celui auquel vous avez attribué le nom de poste et affecter ce terme à la publication que vous avez dans cette catégorie/terme.

Ex.

Créez un terme de taxonomie nommé Post Slug et affectez-le à la publication et essayez ma requête avec vos paramètres respectifs.

1
Bindiya Patoliya