This is not really much of a technical question but it is more like an intro to the topic I want to talk about.
So this is my first time creating an advanced search custom page in which I need to get post according to the custom post type, custom taxonomies, years and the order by.
The problem is that I really have not idea in how to star here. I've been looking for tutorials or snippets in the Internet but everything I find somehow can not work with dropdowns(why?, i don't know), all the tutorials that I have found are about using check-boxes.
So any recommendation or function code you guys can tell me about?.
Thanks in advance.
This is the HTML code I want to use:
<!-- Post Type -->
<div class="col-md-3">
<div class="form-group">
<label for="post_type">Post Type</label>
<select class="form-control">
<option>Any</option>
<option>Post</option>
<option>Portfolio</option>
<option>Snippet</option>
</select>
</div>
</div>
<!-- Taxonomy -->
<div class="col-md-3">
<div class="form-group">
<label for="post_type">Taxonomy</label>
<select class="form-control" multiple>
<option>Any</option>
<option>category</option>
<option>portfolio_categories</option>
<option>snippets_categories</option>
</select>
</div>
</div>
<!-- Year -->
<div class="col-md-3">
<div class="form-group">
<label for="post_type">Year</label>
<select class="form-control">
<option>Any</option>
<option>2018</option>
<option>2017</option>
<option>2016</option>
<option>2015</option>
<option>2014</option>
</select>
</div>
</div>
<!-- Orderby -->
<div class="col-md-3">
<div class="form-group">
<label for="post_type">Order by</label>
<select class="form-control" multiple>
<option>Any</option>
<option>Author</option>
<option>Popularity (# of Comments)</option>
<option>Views post_views_count</option>
<option>Year</option>
<option>ASC</option>
<option>DESC</option>
</select>
</div>
</div>
<!-- Search Button -->
<div class="col-md-12">
<input type="submit" class="btn btn-primary" id="buscar_btn" value="Search">
<noscript><b>Your browser does not support Javascript, this making it unable to display the posts.</b></noscript>
<div id="resultados"><div class="cargando_medio"></div></div>
</div>
Here is the function for the post_view_count custom field:
// Function to display number of views.
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 Views";
}
return $count.' Views';
}
// Function to count views.
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
// Add it to a column in WP-Admin
function posts_column_views($defaults){
$defaults['post_views'] = __('Views');
return $defaults;
}
add_filter('manage_posts_columns', 'posts_column_views');
function posts_custom_column_views($column_name, $id){
if($column_name === 'post_views'){
echo getPostViews(get_the_ID());
}
}
add_action('manage_posts_custom_column', 'posts_custom_column_views',5,2);
The function is not really good, actually is not good but it does the work. I may have to change it later on, especially because I want it to count views per IP address and per day; it currently does it by counting the views per every REFRESH I do in the current page/post.
function
that will search posts based on the selected options? Can you post the code for the advanced searchform
?select
menus; I suppose you'd use the correctvalue
later on?function
for querying the posts based on the options you defined (e.g. post type and taxonomy); however, it doesn't include the part which sorts the results by "Views". Do your posts have a custom field for the "View" part?