Filter and Hooks

SQL Query to get the archive / year by post type

For listing the published year of the particular post type, we can use the SQL as presented below.

<ul id="termsYear">
<?php $years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_posts WHERE post_type = 'media' AND post_status = 'publish' GROUP BY year DESC" );
foreach ( $years as $year ) {?>
	<li><a data-val='<?=$year->year?>' class="year"><?=$year->year?></a></li>
<?php } ?>

Replace the ‘post_type’ with your own post type.

Views: 31

Standard
Filter and Hooks

Filter to set blank value instead of default “—” in the drop down list of contact form 7

  1. Replace all ‘---' in all forms with the same text:

For replacing all  ‘—‘  blank value of   select , add the  below filter in function.php

function kd_wpcf7_form_elements($html) {
    $text = 'Select';
    $html = str_replace('<option value="">---</option>', '<option value="">' . $text . '</option>', $html);
    return $html;
}
add_filter('wpcf7_form_elements', 'kd_wpcf7_form_elements');

We can replace the  $text variable with any text.

2.  Replace the select element blank value with  unique text.

For replacing the select elements blank with unique text add the following filter in function.php

function kd_wpcf7_form_elements($html) {
    function kd_replace_include_blank($name, $text, &$html) {
        $matches = false;
        preg_match('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $html, $matches);
        if ($matches) {
            $select = str_replace('<option value="">---</option>', '<option value="">' . $text . '</option>', $matches[0]);
            $html = preg_replace('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $select, $html);
        }
    }
    kd_replace_include_blank('subject', 'Subject', $html);
    kd_replace_include_blank('country', 'Country', $html);
    return $html;
}
add_filter('wpcf7_form_elements', 'kd_wpcf7_form_elements');

1

Views: 1079

Standard