0

I have this piece of code which works but it's showing only 5 posts (with custom taxonomy) where should be 7. If I hit the tag url, it shows the 7 registries, so the problem it's in this code and its query. I already tried nopaging = true, posts_per_page = -1 numberposts = -1 and setting the last two to fixed numbers, like 7 or 999; but none of this options seem to do the trick.

Hope someone can help me. Thanks.

add_shortcode('gpp_prod', function( $atts, $content = null ){
ob_start();

$atts = shortcode_atts(
    array(
        'category' => '0'
    ), $atts);

extract($atts);

$args = array(
    'post_type'=>'gpp_prod',
    'orderby' => 'title',
    'order' => 'ASC'
);

if($category > 0 ){
    $args['tax_query'] = array(
        array(
            'posts_per_page' => -1,
            'taxonomy' => 'cat_prod',
            'field' => 'term_id',
            'nopaging' => true,
            'terms' => $category
        )
    );
}

?>
<?php $products = get_posts($args);
$termt = get_term($category, 'cat_prod');

<div class="prod-cats">
    <h1><?php echo $termt->name;?></h1>
    <?php if(count($products)>0) {
        foreach ($products as $key => $value) { ?>
        <div class="prod-group">
            <div class="prod-title"><?php echo do_shortcode( $value->post_title); ?></div>
            <div class="prod-low">
                <div class="prod-image"><img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id($value->ID)); ?>"/></div>
                <div role="tabpanel" class="prod-info">
                    <!-- Nav tabs -->
                    <ul class="nav nav-tabs" role="tablist">
                        <li role="presentation" class="active"><a href="#tab-1-<?php echo $value->ID?>" aria-controls="home" role="tab" data-toggle="tab">Desc</a></li>
                        <li role="presentation"><a href="#tab-2-<?php echo $value->ID?>" aria-controls="profile" role="tab" data-toggle="tab">Specs</a></li>
                    </ul>

                    <!-- Tab panes -->
                    <div class="tab-content">
                        <div role="tabpanel" class="tab-pane active" id="tab-1-<?php echo $value->ID?>"><?php echo do_shortcode( $value->post_content); ?></div>
                        <div role="tabpanel" class="tab-pane" id="tab-2-<?php echo $value->ID?>"><?php $meta_value = get_post_meta( $value->ID, 'meta-text', false );
                                               if( !empty( $meta_value ) ) {
                                                   echo $meta_value[0];
                                               } else {
                                                   echo 'No specs available';
                                               }
                            ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php } ?>
    <?php
    }
    ?>
</div>
<?php
wp_reset_postdata();
return ob_get_clean();

});

2 Answers 2

1

The 'posts_per_page' => -1, and 'nopaging' => true, shouldn't be in your tax_query params. That might be what's throwing it off.

Try:

$args = array(
    'post_type'=>'gpp_prod',
    'orderby' => 'title',
    'order' => 'ASC',
    'posts_per_page' => -1,
    'nopaging' => true,
);

and:

$args['tax_query'] = array(
    array(            
        'taxonomy' => 'cat_prod',
        'field' => 'term_id',
        'terms' => $category
    )
);
1

It looks like you're placing the posts_per_page parameter in a wrong place under the tax_query part of get_posts.

You could try for example:

$args = array(
    'posts_per_page' => -1,
    'post_type'      =>'gpp_prod',
    'orderby'        => 'title',
    'order'          => 'ASC'
);

if( $category > 0 ){
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'cat_prod',
            'field'    => 'term_id',
            'terms'    => $category
        )
    );
}

within your code snippet.

Update: You should also avoid using extract(), it's not considered the best PHP practice. It's being removed from the WordPress core: check for example this answer and ticket #22400.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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