Support » Plugin: Attachments » CPT registered, but cant query attachments

  • Resolved junior

    (@epiric)


    Hi, first of all really nice plugin.
    I have registered CPT and that works fine, i have only title + attachment available to it.

    ...........
    $args = array(
    		'labels' => $array,
    		'public' => true,
        'query_var' => true,
    		'has_archive' => true,
    		'hierarchical' => true,
    		'menu_position' => 5,
    		'supports' => array('title')
    	);
    	register_post_type('docs',$args);

    Then i made this so i have the attach button in my ADD/edit CPT DOCS screen

    function my_attachments( $attachments )
    {
      $args = array(
        'post_type'     => array( 'post', 'page', 'dokumenti' ),
      );
    
      $attachments->register( 'my_attachments', $args ); // unique instance name
    }
    
    add_action( 'attachments_register', 'my_attachments' );

    And now i use this to show only attached documents of all posts from that category,

    <?php
                $args = array(
                'post_type' => 'docs',
                'posts_per_page' => 5,
                );
                $loop = new WP_Query( $args );
                if ( $loop->have_posts() ) {
                  while ( $loop->have_posts() ) : $loop->the_post();
    
    ?>
    <?php
    $attachments = new Attachments( 'attachments' );
    ?>
    <?php if( $attachments->exist() ) : ?>
                  <?php while( $attachments->get() ) : ?>
                    <?php
                      $class = f_detectIcon($attachments->subtype());
                     ?>
                    <li>
                      s
                      <a href="<?php echo $attachments->url(); ?>" target="_blank" class="mtli_attachment <?php echo $class; ?>" download >
                        <?php echo $attachments->field( 'title' ); ?> (<?php echo $attachments->filesize(); ?>)</a>
                    </li>
                  <?php endwhile; endif; ?>
    
                  <?php
                  endwhile;
                }
                wp_reset_query();
                ?>

    And if i echo the_title, it works, so WP loop is ok, but none of atachments are displayed. If i hovever replace ‘post_type’ => ‘docs’ to ‘post_type’ => ‘post’ so i read attachments of default POSTS it works. Why cant i see attachments from my CPT :S

    I tried var dump on $attachments, its empty. but when i read default POST in loop its ok, and shows attachments.

    Thanks

    https://wordpress.org/plugins/attachments/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter junior

    (@epiric)

    oh,

    $args = array(
        'post_type'     => array( 'post', 'page', 'dokumenti' ),
      );

    is

    $args = array(
        'post_type'     => array( 'post', 'page', 'docs' ),
      );

    i was renaming variables for sake of post here, so its in english. So i messed up that here in post, so issue is not there.

    Thread Starter junior

    (@epiric)

    Oh i managed to get it to work.

    $attachments->register( ‘my_attachments’, $args );

    Here was the issue whoever needs it, so because of this object creation should be
    $attachments = new Attachments( ‘my_attachments’ );

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘CPT registered, but cant query attachments’ is closed to new replies.