Home › Forums › Add-ons › Flexible Content Field › See how many times each layout is used in all pages › Reply To: See how many times each layout is used in all pages
I would not try to do a loop over that many posts and fields, more then likely it would simply time out. I would do a query directly on the db to get what I want.
global $wpdb;
/*
the following does not use $wpdb->prepare()
when I'm doing queries like this I am in control of all values
and I make sure that everything is correct
also, there is not "JOIN" because there is no difference in performance
between my way to join tables and the """Right Way"""
*/
global $wpdb;
$query = 'SELECT meta_value
FROM '.$wpdb->postmeta.' pm, '.$wpdb->posts.' p
WHERE pm.meta_key = "{FLEX FIELD NAME HERE}"
AND pm.post_id = p.ID
AND p.post_status = "publish"';
$values = $wpdb->query($query);
$counts = array();
if ($values) {
foreach ($values as $value) {
$value = maybe_unserialize($value['meta_value']);
if (!empty($value)) {
foreach ($value as $layout) {
if (!isset($counts[$layout])) {
$counts[$layout] = 0;
}
$counts[$layout]++;
}
}
}
}
echo '<pre>'; print_r($counts); echo '</pre>';
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.