3
\$\begingroup\$

I have written the following Plugin, which adds fields to the Media/Attachment Entries and returns all information in one Shortcode. This solution works, however, is pretty slow the more entries available.

I know that the problem lies in the while loop. The 20000 simply is a high number in order for all posts to be searched through. However, I included the entire code in order to see what I'm trying to achieve:


defined( 'ABSPATH' ) or die( 'Are you ok?' );
/** ----- ADD FIELDS IN MEDIA LIBRARY ----- **/
function additionalMediaFields( $form_fields, $post ) {
    $form_fields['copyright_author'] = array(
        'label' => '© Autor',
        'input' => 'text',
        'value' => get_post_meta( $post->ID, 'copyright_author', true ),            
    );
    $form_fields['copyright_description'] = array(
        'label' => '© Beschreibung',
        'input' => 'text',
        'value' => get_post_meta( $post->ID, 'copyright_description', true ),           
    );
    $form_fields['copyright_website'] = array(
        'label' => '© Website',
        'input' => 'text',
        'value' => get_post_meta( $post->ID, 'copyright_website', true ),           
    );
    return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'additionalMediaFields', 10, 2 );

function storeAdditionalMediaFields( $post, $attachment ) {
    if( isset( $attachment['copyright_author'] ) )
        update_post_meta( $post['ID'], 'copyright_author', $attachment['copyright_author'] );
    if( isset( $attachment['copyright_description'] ) )
        update_post_meta( $post['ID'], 'copyright_description', $attachment['copyright_description'] );
    if( isset( $attachment['copyright_website'] ) )
        update_post_meta( $post['ID'], 'copyright_website', $attachment['copyright_website'] );
    return $post;
}
add_filter( 'attachment_fields_to_save', 'storeAdditionalMediaFields', 10, 2 );

/** ----- SHOW THEM IN ONE SHORTCODE ----- **/
function listAllCopyrightInformation ($atts, $content = null) {

    $i = 1;
    while ( $i<=20000 ) {
        $copyrightAuthor = get_post_meta($i, 'copyright_author', true );
        $copyrightDescription = get_post_meta($i, 'copyright_description', true );
        $copyrightWebsite = get_post_meta($i, 'copyright_website', true );

        if ( !empty($copyrightAuthor)) {
            if( !empty($copyrightDescription)) {
                $CopyrightEntries .= $copyrightDescription . ': ';
            }
            $CopyrightEntries .= '&copy; ';
            $CopyrightEntries .= $copyrightAuthor;
            if( !empty($copyrightWebsite)) {
                $CopyrightEntries .= ' / <a href="https://' . get_post_meta($i, 'copyright_website', true) . '" target="_blank">' . get_post_meta($i, 'copyright_website', true) . '</a> || ';
            }
        }
        $i++;
    }
    $CopyrightReturn = substr($CopyrightEntries, 0, -8);
    return $CopyrightReturn;
}

add_shortcode("show-all-copyrights", "listAllCopyrightInformation");

Thanks in advance. I appreciate your effort!

\$\endgroup\$
1
\$\begingroup\$

If you don't need to worry about WordPress filters on getting post meta, you probably can just do this with a single SQL statement and iterate through the results, rather than relying on WordPress to get the values from the database, since that's 20,000 sql statements, plus the memory of caching all that data (which WordPress does behind the scenes when you get post meta for a post).

\$\endgroup\$

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.