• Resolved dumcoder

    (@dumcoder)


    I’ve been trying so many different ways of counting how many times a word or value is used in a postmeta meta_value string. For example say this is my meta_value
    a:1:{s:3:"d";a:2:{i:0;a:7:{s:9:"super_m";s:10:"superv";s:6:"m";s:1:"4";s:7:"ap";s:2:"PM";s:6:"M";s:2:"10";s:7:"time_AP";s:2:"PM";s:8:"super_m";s:55:"cmp";s:7:"m";s:1:"1";}i:1;a:2:{s:8:"indetail";s:2:"dd";s:7:"m";s:1:"1";}}}

    “super_m” is in there 2 times. How do I go about counting them?

    Where do I go with this. I’ve tried so many variations..
    $test_count = $wpdb->get_var(“SELECT COUNT(*) FROM $wpdb->postmeta ???;”);

Viewing 14 replies - 1 through 14 (of 14 total)
  • Thread Starter dumcoder

    (@dumcoder)

    Now what if I had multiple meta_keys with the same name I am trying to read? Sorry for ignorance, it’s been a long day. Just figured I would look for some help, this has been holding me up a while and I am so burnt out on it.

    Thread Starter dumcoder

    (@dumcoder)

    Lost.

    $dum = get_post_custom_values('_custom_meta');
    $coder = implode(':',$dum);
    $help = unserialize($coder);
    echo substr_count($help, 'super_m');

    Previous working code on older wordpress looked something like this

    <?php
    $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts
    LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
    LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
    WHERE $wpdb->terms.term_id = '9'
    AND $wpdb->postmeta.meta_key = '$day'
    AND $wpdb->postmeta.meta_value != ''
    ");
    echo '(' .$count . ')';?>

    Code executed in header….

    $dum = get_post_custom_values('_custom_meta');
    echo substr_count($dum, 'super_m');
    Thread Starter dumcoder

    (@dumcoder)

    returns 0. I can count 11 in the db.

    I’m sorry, please restate the goal.

    Count all occurences of ‘super_m’ in the database or count all occurences of ‘super_m’ in a custom field for a particular post.

    Also, what is the custom field ‘key’ you are trying to search? Is it “_custom_meta”?

    Thread Starter dumcoder

    (@dumcoder)

    Ok so basically in my wp_postmeta table, I have 4 posts with the meta_key being ‘_custom_meta’.

    Now within those 4 posts, I want to count all occurrences of ‘super_m’.

    So we know that ‘_custom_meta’ = 4

    I need to know how many times ‘super_m’ is written in the contents of the strings.

    <?php
    $var_count = $wpdb->get_var("SELECT count( * ) FROM $wpdb->postmeta WHERE meta_key = '_custom_meta' AND meta_value LIKE '%super_m%';");
    echo '<p>count is ' . $var_count . '</p>';
    ?>
    Thread Starter dumcoder

    (@dumcoder)

    My issue with that is, it only counts how many ‘_custom_meta’s have ‘super_m’ in them. Not how many ‘super_m’s are in the ‘_custom_meta’ meta_key.

    So if I have a ‘_custom_meta’ that contains ‘super_m’ more than once, it only counts it as one.

    <?php
    $count = 0;
    $results = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_custom_meta' and meta_value LIKE '%super_m%';");
    foreach($results as $result) {
      $count = $count + substr_count($result->meta_value, 'super_m');
    }
    echo '<p>count is ' . $count . '</p>';
    ?>
    Thread Starter dumcoder

    (@dumcoder)

    That is beautiful. Thank you very much.

    Now extending on that, is it possible to sort out the super_m value?
    As in count the ‘super_m’s with data?

    So if:
    super_m = ” //don’t count
    super_m = ‘Hello.’ //count

    Your help is very much appreciated.

    Yeah, but I believe you will need to play with something like unserialize to do that…

    Thread Starter dumcoder

    (@dumcoder)

    Alright. Thank you for all of your help. You have saved me hours of frustration.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Calculate occurrences of text word in postmeta meta_value’ is closed to new replies.