Skip to content

Instantly share code, notes, and snippets.

@evgrezanov
Last active July 24, 2020 14:47
Show Gist options
  • Save evgrezanov/badfe5b598f8c942de6f74848f386ac9 to your computer and use it in GitHub Desktop.
Save evgrezanov/badfe5b598f8c942de6f74848f386ac9 to your computer and use it in GitHub Desktop.
<?php
//filter query before request (company with open jobs, pagination, terms matches)
add_filter( 'wpv_filter_query', 'show_only_hiring_now_companies', 99, 3 );
function show_only_hiring_now_companies( $query_args, $view_settings, $view_id ) {
if ( $view_id == 72 ){
// post per pagination page
$post_per_page = 9;
$query_args['posts_per_page'] = $post_per_page;
// add orders by culture value tags count
if ( isset($_REQUEST['wpv-culture-value']) ):
add_filter( 'posts_clauses', 'wpse173949_posts_clauses', 20, 1 );
endif;
// hiring now filter
if ( isset($_REQUEST['wpv-hiring-now']) && $_REQUEST['wpv-hiring-now'] == 'OpenJobs' ):
global $wpdb;
$table_name = 'toolset_associations';
$relation_id = '11';
$column_parent_id = 'parent_id';
$column_child_id = 'child_id';
$myquery = "
SELECT DISTINCT company." . $column_parent_id . " AS company_id
FROM " . $wpdb->prefix . $table_name . " company
INNER JOIN " . $wpdb->posts ." job ON company.child_id = job.ID
AND job.post_status = 'publish'
WHERE company.relationship_id = 11
";
$hiring_now = $wpdb->get_results($myquery);
if (!empty($hiring_now)):
$arg = array(
'numberposts' => -1,
'exclude' => array_column($hiring_now, 'company_id'),
'fields' => 'ids',
'post_type' => 'company'
);
// array ids companies no have open jobs
$no_hiring = get_posts($arg);
if (!empty($no_hiring)):
$query_args['exclude'] = $no_hiring;
endif;
endif;
endif;
// if pagination page
if ( isset($_REQUEST['wpv_paged']) ):
$query_args['paged'] = (int)$_REQUEST['wpv_paged'];
endif;
}
return $query_args;
}
//https://wordpress.stackexchange.com/questions/173949/order-posts-by-tags-count
function wpse173949_posts_clauses( $pieces ) {
remove_filter( 'posts_clauses', 'wpse173949_posts_clauses', 20 );
if ( isset( $pieces['orderby'] ) && 'tags_count' == $pieces['orderby'] ) :
global $wpdb;
$filter_culture = $_REQUEST['wpv-culture-value'];
$culture_tags_count = count($filter_culture);
// culture matches
$taxonomy_name = 'culture-value';
$pieces[ 'fields' ] .= $wpdb->prepare(
", (SELECT COUNT(tr.object_id) FROM " . $wpdb->prefix . "term_relationships tr "
. " JOIN " . $wpdb->prefix . "term_taxonomy AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id "
. " WHERE tr.object_id = " . $wpdb->prefix . "posts.ID AND tt.taxonomy = %s) AS tags_count", $taxonomy_name);
// Treat zero tagged posts as max int (~0).
$pieces[ 'orderby' ] = 'CASE WHEN tags_count THEN (tags_count/'.$culture_tags_count.')*100 ELSE ~0 END DESC';
$pieces['join'] .=<<<SQL
LEFT OUTER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID={$wpdb->term_relationships}.object_id
LEFT OUTER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id)
LEFT OUTER JOIN {$wpdb->terms} USING (term_id)
SQL;
$pieces['where'] .= " AND taxonomy = 'culture-value' ";
endif;
echo "<pre>";
var_dump($pieces);
echo "</pre>";
return $pieces;
}
// display matches at company card
add_shortcode('terms_matches','display_terms_mathes');
function display_terms_mathes($atts) {
// The post data of the current post in the Loop
$post = get_post();
if (isset($_REQUEST['wpv-culture-value'])):
//var_dump($post->tags_count);
// All culture in query
$filter_culture = $_REQUEST['wpv-culture-value'];
// All culture at compani item
$culture_values = get_the_terms($post->ID, 'culture-value');
$current_cultures_slug = array_column($culture_values, 'slug');
$matches_array = array_intersect($filter_culture, $current_cultures_slug);
$matches = count($matches_array)/count($filter_culture)*100;
$output = '<h4 class="h-has-cultures-match"><strong>'.round($matches).'%</strong> match</h4>';
return $output;
endif;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment