Could someone please explain why the first section of code works but not the second. I am trying to filter the content on a website based on what values have been selected in the backend on each post using Advanced Custom Fields.
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}
// only modify queries for 'event' post type
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'property' ) {
// allow the url to alter the query
if( isset($_GET['bedrooms']) ) {
$query->set('meta_key', 'bedrooms');
$query->set('meta_value', $_GET['bedrooms']);
}
}
// return
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');
non working:
add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts( $query ) {
// bail early if is in admin
if( is_admin() ){
return;
}
// get meta query
$meta_query = $query->get('meta_query');
if( isset($_GET['bedrooms']) ){
$meta_query[] = array(
'key' => 'bedrooms',
'value' => $_GET['bedrooms'],
'compare' => '=',
);
}
// update meta query
$query->set('meta_query', $meta_query);
return;
}
Related
I am trying to manipulate an elementor query for all post types. I am using the Profile Builder plugin to restrict content. Their support sent me this function to check wether the post is restricted or not:
wppb_content_restriction_is_post_restricted( $post_id )
When I output the result of this function on the post it is either 1 or nothing. (1 being restricted).
The support also send me a custom addon-plugin of another plugin they have, where I'd only need to add the new function. When I do this and install it to my page, nothing changes though.
Here is the code of their mini plugin:
<?php
/*
Plugin Name: Paid Member Subscriptions - Exclude Restricted Posts From Query
Plugin URI: http://www.cozmoslabs.com
Description: Exclude restricted posts and pages from the main queries like blog, archive and taxonomy pages. It does not exclude them from custom queries.
Author: Cristian Antohe
Version: 1.0
Author URI: http://www.cozmoslabs.com
*/
add_action( 'pre_get_posts', 'pmsc_exclude_post_from_query' );
function pmsc_exclude_post_from_query( $query ) {
remove_action('pre_get_posts', 'pmsc_exclude_post_from_query');
if( !function_exists( 'pms_is_post_restricted' ) || is_admin() || is_single() )
return;
if( $query->is_main_query() || ( $query->is_search() && isset( $_GET['s'] ) ) ) {
$args = $query->query_vars;
$args['suppress_filters'] = true;
$args['posts_per_page'] = get_option( 'posts_per_page' );
$posts = get_posts($args);
$ids = wp_list_pluck( $posts, 'ID' );
$restricted_ids = array_filter($ids,'pms_is_post_restricted');
$query->set( 'post__not_in', $restricted_ids );
}
}
In case that a post is restricted, I want to exclude it from my query. So that the user only sees posts that can be accessed.
Now the challenge is to write a custom query that I can then put inside of the query ID of elementor's post widget to perform my custom query.
My query looks like this so far:
function prevent_restricted_posts_from_loading( $query ) {
if( !function_exists( 'wppb_content_restriction_is_post_restricted' ) || is_admin() || is_single() )
return;
if( $query->is_main_query() || ( $query->is_search() && isset( $_GET['s'] ) ) ) {
$args = $query->query_vars;
$posts = get_posts($args);
$ids = wp_list_pluck( $posts, 'ID' );
$restricted_ids = array_filter($ids,'wppb_content_restriction_is_post_restricted');
$query->set( 'post__not_in', $restricted_ids );
}
}
add_action( 'elementor/query/prevent_restricted_posts_from_loading', 'prevent_restricted_posts_from_loading' );
Departing from the following tutorial: "Columns in WooCommerce", the intention is to make the coupon amount column sortable based on amount.
Making it sortable seems to be successful with my already written code. Nevertheless, the rule to sort goes wrong.
I've tried several things before but the sort code is not applied after clicking,
Who would like to take a closer look?
What I've used so far:
add_filter('manage_edit-shop_coupon_sortable_columns', 'misha_sortable');
function misha_sortable( $sortable_columns ){
$sortable_columns['amount'] = 'amount';
return $sortable_columns;
}
add_action( 'pre_get_posts', 'misha_filter' );
function misha_filter( $query ) {
// if it is not admin area, exit the filter immediately
if ( ! is_admin() ) return;
if( empty( $_GET['orderby'] ) || empty( $_GET['order'] ) ) return;
if( $_GET['orderby'] == 'amount' ) {
$query->set('meta_key', 'amount' );
$query->set('orderby', 'meta_value'); // or meta_value_num
$query->set('order', $_GET['order'] );
}
return $query;
}
The correct metakey is coupon_amount instead of amount
So this should suffice
// Make column sortable
function filter_manage_edit_shop_coupon_sortable_columns( $columns ) {
$columns['amount'] = 'amount';
return $columns;
}
add_filter( 'manage_edit-shop_coupon_sortable_columns', 'filter_manage_edit_shop_coupon_sortable_columns', 10, 1 );
// Fires after the query variable object is created, but before the actual query is run.
function action_pre_get_posts( $query ) {
// If it is not admin area, exit the filter immediately
if( ! is_admin() ) return;
// Get orderby
$orderby = $query->get( 'orderby' );
// Set query
if( $orderby == 'amount' ) {
$query->set( 'meta_key', 'coupon_amount' );
$query->set( 'orderby', 'meta_value_num' );
}
}
add_action( 'pre_get_posts', 'action_pre_get_posts', 10, 1 );
Almost all the guides to implement price sorting on woocommerce site are the same. I've tried two main ways. 1st - hooked up my query args from functions.php like this:
add_filter('woocommerce_get_catalog_ordering_args', 'my_custom_woocommerce_catalog_orderby');
function my_custom_woocommerce_catalog_orderby( $args ) {
if(isset($_GET['sort'])){
if($_GET['sort_by'] == 'by_rating'){
$args['meta_key'] = '_wc_average_rating';
} else if($_GET['sort_by'] == 'by_price') {
$args['meta_key'] = '_price';
}
$args['orderby'] = 'meta_value_num';
$args['order'] = strtoupper($_GET['sort']);
}
return $args;
}
var_dump shows that it has been catched:
array(3) { ["orderby"]=> string(14) "meta_value_num" ["order"]=> string(4) "DESC" ["meta_key"]=> string(6) "_price" }
But products are still sorted by default woocommerce setting (price, asc).
Then i implemented my filter plugin (which is working pretty well) and tried to add this sorting query to it.
class My_blah_blah_filter {
public static $instance;
public $productquery;
public static function init() {
if ( is_null( self::$instance ) ) {
self::$instance = new My_blah_blah_filter ();
}
return self::$instance;
}
private function __construct() {
add_action( 'woocommerce_product_query', [ $this, 'filter_by_current_range' ] );
}
public function filter_by_current_range( $query ) {
$this->productquery = $query;
$query->set('tax_query', $this->get_tax_query());
if(isset($_GET['sort'])){
if($_GET['sort_by'] == 'by_rating'){
$query->set('meta_key', '_wc_average_rating');
} else if($_GET['sort_by'] == 'by_price') {
$query->set('meta_key', '_price');
} else {
$query->set('meta_key', '_price');
}
$query->set('orderby', 'meta_value_num');
$query->set('order', strtoupper($_GET['sort']));
}
}
public function get_tax_query() {
// pa_space-overall
$tax_query = [];
if( isset( $_GET['max_so'] ) && isset( $_GET['min_so'] ) ) {
$terms = get_terms([
'taxonomy' => 'pa_space-overall',
]);
$rlt = [];
foreach( $terms as $term ) {
if( (int) $term->name <= (int) $_GET['max_so'] && (int) $term->name >= (int) $_GET['min_so'] ) {
$rlt[] = $term->term_id;
}
}
$tax_query = [
'relation' => 'AND',
[
'taxonomy' => 'pa_space-overall',
'terms' => $rlt,
'operator' => 'IN',
],
];
}
if( isset( $this->productquery->tax_query ) ) {
$tax_query = array_merge( $this->productquery->query_vars['tax_query'], $tax_query );
}
return $tax_query;
}
Some stuff here is hardcoded, but its not the case, its working. But not the ordering. Its still being catched, added to query, but order is default anyways (price, asc - from woocommerce settings)
The only plugin except woocommerce - is advanced custom fields, which has nothing to do with it.
Cant find any solution anywhere else. Any suggestions? Thanks in advance.
Sorry for poor english or not-perfectly-formatted code
In advance many thanks for read this question!
I have order my post by a custom field (eslora), but when apply the filter (category) this sort is broken.
Some one know how can order my post on this way by default.
Best regards!
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}
// only modify queries for 'event' post type
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'post' ) {
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'eslora');
$query->set('order', 'ASC');
}
// return
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');
I have a search form containing select fields. The first two are populated with custom taxonomies and the third with the default wordpress categories. When using the first two only for a query it works fine. When I use the third( the categories), the search query just ignores the field and comes up with the same results. How can I fix this?
I've used these functions to make them work:
function ftiaxnospiti_filter_search($query) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( $query->is_search ) {
$query->set( 'post_type', array('post', 'seller') );
}
return $query;
};
add_action('pre_get_posts', 'ftiaxnospiti_filter_search');
function ftiaxnospiti_add_custom_types_to_tax( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
// Get all your post types
$post_types = array( 'post', 'seller' );
$query->set( 'post_type', $post_types );
}
return $query;
}
add_action( 'pre_get_posts', 'ftiaxnospiti_add_custom_types_to_tax' );