Wordpress Olam theme - Want to change search function - wordpress

I want to change word-press search logic. So where can I change for customization of wp_query. Below is standard search.
$args = array('s'=> get_search_query(),
'meta_key' => 'edd_price',
'order' => 'ASC',
'orderby' => 'meta_value_num meta_value ',
'post_type' => 'download',
'posts_per_page'=>$posts_per_page_olam,
'paged' => $paged,
'tax_query' => $taxQuery);

If you want to change the search on your site, please hook on the search query like so :
<?php add_action( 'pre_get_posts', 'func_53407889' );
function func_53407889( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_search ) {
// Change here whatever you want using the WP_Query's args
// $query->set( 'post_type', 'event' );
}
}

Related

Wordpress Custom Tax for CPT Paging 404?

I have googled this issue for the last 4 days, and everything I have found has not fixed the issue.
My Custom Post Type's Custom Taxonomy template paging 404's unless I set the sites Blog posts per page to 1, which I cannot do, due to the fact that there will be a need to show more than 1 "blog post" per page...
How can I correct this issue?
LATEST FIX ATTEMPT
// fix the paging 404's on category/tag pages
add_action( 'pre_get_posts', function( $qry ) {
$_num_per_page = ( get_option( 'gyokb_option_name' )['gyokb_articles_pp'] ) ?? 5;
if( ( is_tax( 'kb_categories' ) || is_tax( 'kb_tags' ) ) && ! is_admin() ) {
$qry->set ( 'post_type', array( 'post', 'gyo_kb' ) );
$qry->set( 'posts_per_page', $_num_per_page );
}
} );
My CPT is here... (too much code to paste here...)
https://pastebin.com/SyKJwnCH
My Taxonomy is here...
https://pastebin.com/4BWWgGWq
And My Paging in the template:
<?php
echo paginate_links( array(
'format' => 'page/%#%',
'prev_text'=>' Previous ',
'next_text'=>' Next ',
'current' => max( 1, $page ),
'total' => $pg_ct,
'type' => 'plain',
'mid_size' => 4,
'paged' => $page, ) );
?>
with my WP_Query pulling it all together....
$args = array( 'post_type' => 'gyo_kb', 'posts_per_page' => $_num_per_page, 'paged' => $page,
'orderby' => 'date','order' => 'DESC',
'tax_query' => array(
array (
'taxonomy' => 'kb_categories',
'field' => 'slug',
'terms' => $cur_cat -> slug,
)
),
);
$the_query = new WP_Query( $args );
$page is populated with: $page = get_query_var( 'page' ); $page = ( ! empty( $page ) ) ? $page : 1;
All other variables are populating properly
I had the fix applying during the incorrect "boot" timing for wordpress. Initially I had placed in in the "wp" action.
I moved it to the "init" action, and now it works.

Sort products by most viewed

I'm using a plugin that already count product views and store it in a table called 'mwb_wpr_data'.
The query to list the product views is:
SELECT DISTINCT('productid') FROM 'mwb_wpr_data' WHERE 'action' = 'view'
The productid field is a FK to Woocomerce products.
How can I modify the Woocommerce default sorting, so it will display products by order of most views based in the table 'mwb_wpr_data'?
Current code using the plugin Post View Counter:
add_action( 'pre_get_posts', 'my_view_filter' );
function my_view_filter($query){
if (
$query->is_main_query() &&
( $query->is_home() || $query->is_archive() || $query->is_search() )
) {
$query->set('suppress_filters', 'false');
$query->set('orderby', 'post_views');
$query->set('order', 'DESC');
}
}
According to me you have set the correct pre_get_post but problem is in your if condition.
You have set the current_user_can which not correct filter is for every user so if you are not login in with administrator role than your query will not work.
current_user_can('administrator')
Remove this above one from the condition.
add_action( 'pre_get_posts', 'my_view_filter' );
function my_view_filter($query){
if ($query->is_main_query() && ( $query->is_home() || $query->is_archive() || $query->is_search() )
) {
$query->set('suppress_filters', 'false');
$query->set('orderby', 'post_views');
$query->set('order', 'DESC');
}
}
Ok, so the RIGHT way to do this would be
Change how your post view counts are stored, so that they are stored as postmeta of each product (post) in a field like _post_views_count.
Use meta_key & orderby in wp_query like this
$args = array(
'meta_key' => '_post_views_count',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
< EDIT >
IF you want to use the same plugin for recording your product views, I'd set up a function that fires every time a post view is added to the table that updates a postmeta field of the product.
Is it elegant? No.
Will it work? Yes.
Not sure about the plugin you're using (it would be great if you give additional info about it) but I've been using Post Views Counter for years and it works really well.
It even has a query parameter for WP_Query (to query posts by post views):
EDIT: This query parameter won't work without the Post Views Counter plugin as it's not a WordPress default parameter.
More info about this plugin API here:
https://dfactory.eu/docs/post-views-counter/developers-api/
$query_args = array(
'posts_per_page' => 12,
'order' => 'DESC',
'suppress_filters' => false, //required param
'orderby' => 'post_views', //required param
);
$query = new WP_Query( $query_args );
if ( $query->have_posts() ):
while ( $query->have_posts() ): $query->the_post();
//Post content here
echo get_the_title();
endwhile;
endif;
I hope this helps,
Cheers!
According to the plugins documentation you are required to pass the post_type as well in order for this to work, i.e.
$args = array(
'order' => 'asc',
'post_type' => 'event',
// required by PVC
'suppress_filters' => false,
'orderby' => 'post_views',
'fields' => ''
);
$most_viewed = get_posts( $args );
That code is in the post that #Marounm linked to in comment higher up.
Try passing the post_type arg as well i.e.
$query->set('post_type', 'product');
If you want to showing the the products which was most viewed , so you can follow these below steps .
Steps :1 I have add menu in admin panel using (add_menu_page) function. in function file
function admin_manage_users22(){
add_menu_page('My Page Title', 'MVP', 'manage_options', 'MVP_backend_view',
'MVP_backend_view' );
}
add_action('admin_menu', 'admin_manage_users22');
Step :2 Get product details after user view product it's +1 counter value in wp_postmeta with post id. default value set 1
function MVP_product_details($product ) {
$post_id = get_the_ID();
$count_key = 'AK_product_view_count';
$count = get_post_meta( $post_id, $count_key, true );
if ( empty( $count ) ) {
delete_post_meta( $post_id, $count_key );
update_post_meta( $post_id, $count_key, '1' );
echo "count null";
}else{
$count ++;
update_post_meta( $post_id, $count_key, (string) $count );
}
}
add_filter( 'woocommerce_quantity_input_args', 'MVP_product_details', 10, 2 );
Step :3 Now you can get most viewed product details simply using below code.
function MVP_backend_view(){
$count_key = 'AK_product_view_count';
$query_args = array(
'posts_per_page' => 3,
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_key' => $count_key,
);
$query_args['meta_query'] = array(
array(
'key' => $count_key,
'value' => '0',
'type' => 'numeric',
'compare' => '>',
),
);
$zwcmvp_query = new WP_Query( $query_args );

woocommerce - list hidden products

I am working on a project where I need to list all hidden products in a page. I have created a shortcode for that and I have used following meta query, but it's not working.
$meta_query = array(
'key' => '_visibility',
'value' => array('hidden'),
'compare' => 'IN'
);
Any idea why this is not working? Thank you in advance for your help.
$args = array(
'post_type' => 'product',
'meta_key' => '_visibility',
'meta_value' => 'hidden'
);
$query = new WP_Query($args);
Then your usual loop worked for me :)
WooCommerce 3+ visibility has been changed to a taxonomy instead of post meta and if you're using WC()->query->get_tax_query() function call for building the args, you can just remove that call from setting tax_query.
IF you don't have access to the args or how they are built and it is ran through get_tax_query you can use the filter to remove the NOT IN tax query for visibility:
add_filter( 'woocommerce_product_query_tax_query', 'smyles_show_all_products' );
// do something that calls some fn that makes product query
remove_filter( 'woocommerce_product_query_tax_query', 'smyles_show_all_products' );
function smyles_show_all_products( $tax_query ) {
foreach( (array) $tax_query as $t_index => $t_query ){
if( isset( $t_query['taxonomy'], $t_query['operator'] ) && $t_query['taxonomy'] === 'product_visibility' && $t_query['operator'] === 'NOT IN' ){
unset( $tax_query[ $t_index ] );
break;
}
}
return $tax_query;
}
Use this
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array( 'catalog', 'visible' ),
'compare' => 'IN'
)
)

How to random order a user list with get_users

I actually have a "users" page ordered by post_count.
I'm wondering if is possible to order the list randomly. I know that the get_usersfunction just allows ordering by 'ID', 'login', 'nicename', 'email', 'url', 'registered', 'display_name', 'post_count', 'include',or'meta_value'
For get_posts, if I remember well, there is a rand option to achieve this.
Here's the code of my template:
<?php
/*
Template Name: Display Contributors and Authors
*/
$args = array(
'role' => 'contributor',
'orderby' => 'post_count',
'order' => 'DESC'
);
// only return users with published posts
$args['has_published_posts'] = true;
// run the WP_Query
$contributors = get_users( $args );
?>
You can use following code to register order by rand:
add_action( 'pre_user_query', 'my_random_user_query' );
function my_random_user_query( $class ) {
if( 'rand' == $class->query_vars['orderby'] )
$class->query_orderby = str_replace( 'user_login', 'RAND()', $class->query_orderby );
return $class;
}
And then use it like this:
$args = array(
'role' => 'contributor',
'orderby' => 'rand',
'order' => 'DESC'
);
Source

Group Posts by meta_query

I'm trying to group my posts by a meta value, and can't seem to figure it out. My query below just returns "featured" posts. I would like the query to return all posts, but have the "featured" results before the rest.
Any ideas?
$args = array(
'posts_per_page' => '20',
'paged' => $current_page,
'meta_query' => array(
array(
'key' => 'tags',
'value' => 'featured',
'compare' => 'like'
)
),
'order_by' => 'date',
'order' => 'DESC'
);
Your answer is rewind_posts(). Run your query to get all the posts, run your loop and filter out all posts which is not featured, rewind the loop, rerun the loop and filter out featured posts.
Something like this will do
$args = [
// Your query arguments to get all postst
];
$q = new WP_Query( $args );
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
if ( get_post_meta( $post->ID, 'tags', true ) == 'featured' ) {
// Run your loop for featured posts
}
}
$q->rewind_posts(); // Rewind the loop to rerun it
while ( $q->have_posts() ) {
$q->the_post();
if ( get_post_meta( $post->ID, 'tags', true ) != 'featured' ) {
// Run your loop for non featured posts
}
}
wp_reset_postdata();
}

Resources