• Resolved BibiDeCarli

    (@bibidecarli)


    Ok, I ‘ve a taxonomy archive page, with multiple loops and I’m trying to show all attachments with this term.

    But I’m getting this error code AFTER the attachments:
    Fatal error: Call to a member function have_posts() on a non-object in… I’m not very good with PHP. Any help, pleaseee?

    <?php $queried_object = get_queried_object();
        $term_id = $queried_object->term_id; 
    
    	$args = array(
    	'post_status' => 'inherit',
    	'numberposts' => 0,
    	'post__not_in' => array_merge($do_not_duplicate,get_option( 'sticky_posts' )),
    	'post_type' => 'attachment',
    	);
    
    	$args['tax_query'] = array(
    	array(
    		'taxonomy' => 't-arte',
    		'terms' => $term_id,
    		'field' => 'id',
    		),
    		);
    
    	$media_query = new WP_Query( $args)?>
    
        <?php  if($media_query->have_posts()) : while ($media_query->have_posts() ) : $media_query->the_post();
    	if( $post->ID == $do_not_duplicate ) continue; ?>
    
        <?php $t = $data['t-arte'];
    	$array = explode(" ", $t);
    	$array = array_unique($array);?>
    
    	<?php $media_query = array_unique($array); ?>
    
    	<div id="archivespage-media-item">   
    
                <?php $attachments = get_posts( $args );
                 if ( $attachments ) {
                 foreach ( $attachments as $attachment ) {
                 echo '<div id="imagem">';
                 the_attachment_link( $attachment->ID, true );
                 echo '</div>';
                 $do_not_duplicate[] = $post->ID; }
                 }?>
    
        </div>
    
        <?php endwhile; else: ?>	
    
         //do stuff       
    
         </div>
    
         <?php endif; ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter BibiDeCarli

    (@bibidecarli)

    I’ve tried change it for this, but now its showing the attachments results by 5 times instead of one O_o

    <?php $queried_object = get_queried_object();
        $term_id = $queried_object->term_id; 
    
    		$args = array(
    		'post_status' => 'inherit',
    		'numberposts' => 0,
    		'post__not_in' => array_merge($do_not_duplicate,get_option( 'sticky_posts' )),
    		'post_type' => 'attachment',
    		);
    
    		$args['tax_query'] = array(
    		array(
    			'taxonomy' => 't-arte',
    			'terms' => $term_id,
    			'field' => 'id',
    		),
    		); ?>
    
            <?php $t = $data['t-arte'];
    	$array = explode(" ", $t);
    	$array = array_unique($array);?>
    
    	<?php $media_query = array_unique($array); ?>
    
    		<?php $media_query = get_posts($args);
            if( !empty( $media_query ) ) :
            foreach ($media_query as $media_query) :
    	global $post; $post = $media_query;
            setup_postdata($media_query);
            ?>
    
    	<div id="archivespage-media-item">   
    
    		<?php $attachments = get_posts( $args );
                             if ( $attachments ) {
                                foreach ( $attachments as $attachment ) {
                                   echo '<div id="imagem">';
                                   the_attachment_link( $attachment->ID, true );
                                   echo '</div>';
                                   }
                             }?>
    
        </div>
    
        <?php endforeach;else :?>       
    
         <p>Ainda n?o temos nenhuma imagem relacionada :(</p>  
    
         </div>
    
         <?php endif; ?>
                <?php wp_reset_query();?>
    Thread Starter BibiDeCarli

    (@bibidecarli)

    If anyone is interested, I’ve made these and now its working nicely. Shows all attachments for a specific term in archive’s page. ??

    global $wp_query;
    
    // convert query object to array
    $original_query = (array) $wp_query;
    
    // attach query parameters to original query
    $attach_query = array(
                   'post_type'=> array( 'attachment' ),
                   'post_status' => array( null )
    );
    
    // merge both arrays
    $args = array_merge($original_query['query_vars'], $attach_query);
    
    query_posts( $args );

    https://wordpress.stackexchange.com/questions/29635/how-to-create-an-attachments-archive-with-working-pagination

    Thread Starter BibiDeCarli

    (@bibidecarli)

    Sorry, I’ll post all the code again:

    <?php $queried_object = get_queried_object();
    $term_id = $queried_object->term_id; 
    
    global $wp_query;
    
    $original_query['tax_query'] = array(
    	array(
    	'taxonomy' => 't-arte',
    	'terms' => $term_id,
    	'field' => 'id',
    	),
    	);
    
    $original_query = (array) $wp_query;
    
    	$attach_query = array(
            'post_type'=> array( 'attachment' ),
            'post_status' => array( null )
    );
    	$args = array_merge($original_query['query_vars'], $attach_query);
    
    	$media_query = new WP_Query( $args )?>
    
    	<?php  if($media_query->have_posts()) :
    	while ($media_query->have_posts() ) : $media_query->the_post();
    	if( $post->ID == $do_not_duplicate ) continue; ?>
    
    	<div id="archivespage-media-item">   
    
    	<div id="imagem">
    	<?php echo wp_get_attachment_link($attachment->ID, 'bigger-thumb');?>
             </div>
    
    	</div>
    
    	<?php endwhile; else: ?>	
    
    	//do stuff       
    
    	 </div>
    
    	<?php endif; ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Call to a member function have_posts() on = Taxonomy archive query attachments’ is closed to new replies.