How can I solve all attachment errors in the current log recorded by WordPress?

Source: Internet
Author: User
Tags sprintf

Here we will analyze it a little:
Principle:

Regardless of the version of wordpress, the principle is the same, and it is queried through this core code.

The code is as follows: Copy code

$ Args = array (
'Post _ parent' => $ post-> ID,
'Post _ type' => 'attachment ',
'Post _ mime_type '=> 'image ',
'Posts _ per_page '=>-1,
'Orderby' => 'menu _ order ',
'Order' => 'asc ',
);

$ Attachments = get_posts ($ args );

However, after wordpress 3.6, we encapsulated a method get_attached_media for everyone. So far, the query will become as simple as below.

// Obtain all types of attachments
$ Attachments = get_attached_media ('', $ post-> ID );

// Obtain all audio attachments:
$ Attachments = get_attached_media ('Audio', $ post-> ID );

Is that OK?

You can perform this test:

Insert 5 images into a log and save them;
Then, delete two images from these images, modify and save them (at this time, there are only three images in the log)
Finally, use this function to check whether the output contains five post data records.

Why?

This code queries the data with the post_parents field post_ID and post_type as attachment in the wp_posts table, which causes two problems.

Only insert images are counted, and deleted images are not counted.
The field type of post_parents is BIGINT, which can only be counted once. For example, if you have inserted image I in log A and then inserted image I in log B, you cannot make statistics at the same time.

In this case, the wordpress statistics on the current log attachments are quite inaccurate.
How did I fix it?

How many questions have I considered?

What is used to record the attachments of the current log?
Where to make statistics
How to retrieve

For the log record attachments, I chose the postmeta table to record and add a meta_key. As for where to collect statistics, I chose the hook save_post. The overall code is as follows:

The code is as follows: Copy code

Add_action ('SAVE _ post', 'SAVE _ post_my_func ', 10, 2 );

Function save_post_my_func (){
Global $ wpdb;

$ Content = $ post-> post_content;
If (! Stristr ($ content, '/wp-content/uploads /'))
    {
// If there is no attachment link in the current log, I will update this field to null
Update_post_meta ($ post_id, '_ wp_post_attach_total _','');
Return;
    }

$ Data = array ();
If (preg_match_all ('/(href | src) = [^>] +/wp-content/uploads/(sites/d + /)? ([^ "'> S] +) ["'> s]/is ', $ content, $ match ))
    {
// Count all img with attachment links and links
$ Files = array_flip ($ match [3]);
$ SQL = sprintf ("SELECT 'post _ id' FROM '% s' WHERE 'meta _ key' =' _ wp_attached_file 'AND ('meta _ value' =' % s' ); ",
$ Wpdb-> postmeta, implode ("'OR 'meta _ value' ='", $ files ));

(FALSE! = ($ Row = $ wpdb-> get_col ($ SQL) & $ data = $ row;
    }

// Album in statistics logs
If (preg_match_all ('/?) ([^ "'] *) 1 s *]/is', $ content, $ gallery ))
    {
$ Where = array ();
$ Gallery = explode (',', implode (',', $ gallery [2]);
Foreach ($ gallery as $ val)
        {
$ Where [] = ''post _ id' = '. trim ($ val );
        }

$ SQL = sprintf ("SELECT 'post _ id' FROM '% s' WHERE 'meta _ key' =' _ wp_attached_file 'AND (% s );", $ wpdb-> postmeta, implode ('OR', $ where ));
(FALSE! = ($ Row = $ wpdb-> get_col ($ SQL) & $ data = array_merge ($ data, $ gallery );
    }

// Calculate the thumbnails in the current log
If (FALSE! = ($ Thumb_id = get_post_meta ($ post_id, '_ thumbnail_id ')))
    {
$ Data [] = $ thumb_id [0];
    }

// For future extension and open extension to other applications, here I create a filter hook
$ Data = apply_filters ('WP _ post_attach_total ', $ data, $ post_id );
If (! Empty ($ data ))
    {
$ Data = array_flip ($ data ));
Update_post_meta ($ post_id, '_ wp_post_attach_total _', implode (',', $ data ));
    }
}

You can use the following code to obtain the current log attachment.

The code is as follows: Copy code

Get_post_meta ($ post_id, '_ wp_post_attach_total _');

Why is this research necessary?
Original intention

It was originally necessary to count the number of attachments in wordpress that could not be used after being uploaded, but it was found that the functions provided by wordpress were faulty.
Follow-up

I will make a plug-in to clean up wordpress in the future. Please wait.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.