0

I have the following code which is used to display download posts by the custom taxonomy. I am receiving the error

Object of class WP_Query could not be converted to int

When I have Query Monitor enabled but I am struggling to work out exactly which bit is causing it as the actual code works and displays the correct files in the correct groupings.

I am wondering if it needs to be simplified somehow but if anyone can help me spot the problem I would be very grateful.

Thanks in advance

function downloads_shortcode( $atts ) {

extract(shortcode_atts(array(
    'type' => 'assoc-downloads',
    'posts' => -1,
    'download_type' => '',
    'category' => '',
    'post_id' => '',
), $atts));


$taxonomies = get_object_taxonomies( $type ); ?>
<article class="page type-page status-publish entry"><?php
foreach( $taxonomies as $taxonomy ) :

    $terms = get_terms( $taxonomy, array ('order' => 'DESC', 'orderby' =>'title'));

    foreach( $terms as $term ) :
                $args = array(
                    'post_type' => $type,
                    'posts_per_page' => $posts,  //show all posts
                    'order' => 'ASC',
                    'orderby' => 'title',
                    'download_type' => $download_type,
                    'category_name' => $category,
                    'post_id' => $post_id,
                    'tax_query' => array(
                        array(
                            'taxonomy' => $taxonomy,
                            'field' => 'slug',
                            'terms' => $term->slug,
                        )
                    )
                );
                $posts = new WP_Query($args);

                if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();

                    $attachment_id = get_field('document');
                    $url = wp_get_attachment_url( $attachment_id );
                    $title = get_the_title();
                    // part where to get the filesize
                    $filesize = filesize( get_attached_file( $attachment_id ) );
                    $filesize = size_format($filesize, 2); ?>

                    <section class="category-section">
                        <div class="row">
                            <div class="span12">
                                <h1 class="mid-heading"><?php echo $term->name; ?></h1>
                            </div>

                    <?php echo '<div class="ecd-single"><p><i style="font-size:120%; margin: 0px 6px 0px 3px;opacity:0.5" class="fa fa-file"></i><a href="'.$url.'" title="'.$title.'" class="ecd-download-file">' .$title.' </a> ('.$filesize.') <i  style="font-size:120%; margin: 2px 0 0 5px;opacity:0.5" class="fa fa-download"></i> <a href="'.$url.'">Download</a></p></div></div></section>';
                endwhile; endif;
    endforeach;
endforeach;
echo '</article>';}
0

I rewrote the function based on a few other posts I have seen online and now have the following code which works without any errors.

I am still open to any further suggestions on the best way to streamline this code even further it it can be.

Thanks again

function downloads_shortcode($atts){

extract(shortcode_atts(array(
    'download_type' => ''
), $atts));

// get the terms
$terms = get_terms( 'download_type' );

// no terms?  bail.
if( ! $terms ) return '';

$out = '';

// loop through the terms
foreach( $terms as $term )
{
    // get videos in each term
    $downloads = get_posts(array(
        'post_type' => 'assoc-downloads',
        'download_type' => $download_type,
        'tax_query' => array(
            array(
                'taxonomy'  => 'download_type',
                'field'     => 'id',
                'terms'     => $term->term_id,
                'operator'  => 'IN'
            )
        )
    ));

    // no downloads? continue!
    if( ! $downloads ) continue;

    $out .= '<h2>' . esc_html( $term->name ) . '</h2>';
    $out .= '<ul>';
    // loop through the download posts
    foreach( $downloads as $d )
    {
        $attachment_id = get_field('document', $d);
        $url = wp_get_attachment_url( $attachment_id );
        $title = get_the_title($d);
        // part where to get the filesize
        $filesize = filesize( get_attached_file( $attachment_id, $d ) );
        $filesize = size_format($filesize, 2);

        $out .= '<div class="ecd-single"><p><i style="font-size:120%; margin: 0px 6px 0px 3px;opacity:0.5" class="fa fa-file"></i><a href="'.$url.'" title="'.$title.'" class="ecd-download-file">' .$title.' </a> ('.$filesize.') <i  style="font-size:120%; margin: 2px 0 0 5px;opacity:0.5" class="fa fa-download"></i> <a href="'.$url.'">Download</a></p></div>';
    }
    $out .= '</ul>';
}
return $out;}
  • This also works for just showing a specific category using the download_type option in the shortcode. Would be great to find out the best way to also have an ID option for the shortcode to just show a specific download result. Currently I have another shortcode to do this. – James Jun 19 '16 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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