• Resolved Bloke

    (@bloke)


    I am working on a function to display meta data. I need to combine two arrays so I can output them withe URL and the name.

    global $post;
    	
    $attachmenturl = get_post_meta( $post->ID, 'wcpoa_attachment_ext_url', true);
    $attachmentname = get_post_meta( $post->ID, 'wcpoa_attachment_name', true);	
    $args = array(
        'posts_per_page'   => -1,
        'orderby'          => 'post_date',
        'order'            => 'DESC',
        'post_status'      => 'publish',
        'meta_query' => array(
            array(
                'key'     => 'wcpoa_attachment_ext_url',
                'compare' => 'EXISTS',
            ),
    		 array(
                'key'     => 'wcpoa_attachment_name',
                'compare' => 'EXISTS',
               
            ),
        ),
    );
    	
    foreach($attachmenturl as $key=>$val)
    {
    	echo $val . "<br />";
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • Could you expand on your code a bit please? What does $attachmenturl hold? Why are you showing us WP_Query $args? Why are you getting $attachmentname?

    Thread Starter Bloke

    (@bloke)

    $attachmenturl holds:
    
    0 => string 'https://some_long_url_path/test.zip' (length=66)
      1 => string 'https://some_long_url_path/somefile.pdf' (length=84)
      2 => string 'https://some_long_url_path/file.pdf'' (length=68)
    
    $attachmentname holds:
     0 => string 'Catalog' (length=12)
      1 => string 'Brochure' (length=12)
      2 => string 'Cover Image' (length=12)
    
    So I need each url and file name listed together.
    

    The best you can do is hope the arrays align to the correct files and pair them by Key since there’s no other pairing information.

    $attachments = array();
    
    // Check if the two arrays match
    if( count( $attachmentname ) === count( $attachmenturl ) ) {
    
    	foreach( $attachmenturl as $key => $val ) {
    		
    		// Ensure our key exists in the name array
    		if( isset( $attachmentname[ $key ] ) ) {
    			
    			$attachments[] = array(
    				'name' => $attachmentname[ $key ]
    				'url'  => $val
    			)
    			
    		}
    		
    	}
    	
    }
    
    // Display the attachments
    if( ! empty( $attachments ) ) {
    	
    	foreach( $attachments as $arr ) {
    		
    		printf( '%1$s file url is: %2$s',
    			$arr['name'],
    			$arr['url']
    		);
    		
    	}
    	
    }

    The $attachments variable is an array of arrays. So $attachments[0]['name'] would display the first attachments name and $attachments[0]['ur'] would display the first attachments URL.

    Thread Starter Bloke

    (@bloke)

    Thank you so much. This is just what I needed. I made some small formatting changes to have the file name show with active link to the file.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘output value of meta query arrays’ is closed to new replies.