Execute function if user logged in - Wordpress - php

First time I post here ! Hope my question will be relevant.
I defined a function to exclude a category in a calendar which works well when I execute it like this:
function tribe_exclude_events_category_month_list( $query ) {
if ( isset( $query->query_vars['eventDisplay'] ) && ! is_singular( 'tribe_events' ) ) {
if ( $query->query_vars['eventDisplay'] == 'list' && ! is_tax( Tribe__Events__Main::TAXONOMY ) || $query->query_vars['eventDisplay'] == 'month' && $query->query_vars['post_type'] == Tribe__Events__Main::POSTTYPE && ! is_tax( Tribe__Events__Main::TAXONOMY ) && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'tax_query', array(
array(
'taxonomy' => Tribe__Events__Main::TAXONOMY,
'field' => 'slug',
'terms' => array( 'reunions'),
'operator' => 'NOT IN'
)
) );
}
}
return $query;} add_action( 'pre_get_posts', 'tribe_exclude_events_category_month_list' );
But what I want is to execute it if only if user is logged in.
So I did this :
function tribe_exclude_events_category_month_list( $query ) {
if ( isset( $query->query_vars['eventDisplay'] ) && ! is_singular( 'tribe_events' ) ) {
if ( $query->query_vars['eventDisplay'] == 'list' && ! is_tax( Tribe__Events__Main::TAXONOMY ) || $query->query_vars['eventDisplay'] == 'month' && $query->query_vars['post_type'] == Tribe__Events__Main::POSTTYPE && ! is_tax( Tribe__Events__Main::TAXONOMY ) && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'tax_query', array(
array(
'taxonomy' => Tribe__Events__Main::TAXONOMY,
'field' => 'slug',
'terms' => array( 'reunions'),
'operator' => 'NOT IN'
)
) );
}
}
return $query; }
function exclude_category_when_logged_in() {
if ( is_user_logged_in() ) {
add_action( 'pre_get_posts', 'tribe_exclude_events_category_month_list' );
}} add_action( 'init', 'exclude_category_when_logged_in' );
But this doesn't seem to work.
Could you help please ? Thanks a lot Lucas

Try this something like this
if (is_user_logged_in()){
}else{
}
Here is your full code
if (is_user_logged_in()){
function tribe_exclude_events_category_month_list( $query ) {
if ( isset( $query->query_vars['eventDisplay'] ) && ! is_singular( 'tribe_events' ) ) {
if ( $query->query_vars['eventDisplay'] == 'list' && ! is_tax( Tribe__Events__Main::TAXONOMY ) || $query->query_vars['eventDisplay'] == 'month' && $query->query_vars['post_type'] == Tribe__Events__Main::POSTTYPE && ! is_tax( Tribe__Events__Main::TAXONOMY ) && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'tax_query', array(
array(
'taxonomy' => Tribe__Events__Main::TAXONOMY,
'field' => 'slug',
'terms' => array( 'reunions'),
'operator' => 'NOT IN'
)
) );
}
}
return $query;
}
add_action( 'pre_get_posts', 'tribe_exclude_events_category_month_list' );
}else{
//leave empty
}

Related

pre_get_posts with meta_query effects static pages in WooCommerce

I used a big part of this code to add a meta_query in the WooCommerce product query. I don't get any errors, but the products are NOT getting filtered by the meta. It still shows all products.
function yl_custom_product_query( $query ) {
// check if the user is requesting an admin page
// or current query is not the main query
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
global $product;
$blog_id = get_current_blog_id();
if ($blog_id == '1') {
$subsite = '_visibility_3rdmillennium';
} elseif ($blog_id == '2') {
$subsite = '_visibility_fight2win';
} elseif ($blog_id == '3') {
$subsite = '_visibility_muaythai';
} elseif ($blog_id == '4') {
$subsite = '_visibility_taekwondo';
} elseif ($blog_id == '5') {
$subsite = '_visibility_xprtfightgear';
} elseif ($blog_id == '6') {
$subsite = '_visibility_hayabusashop';
} elseif ($blog_id == '7') {
$subsite = '_visibility_kmushop';
}
$meta_query = array();
// add meta_query elements
if ( !empty( get_query_var( $subsite ) ) ) {
$meta_query[] = array( 'key' => $subsite, 'value' => get_query_var( 'yes' ), 'compare' => 'LIKE' );
}
// unused meta_query for now
if ( !empty( get_query_var( 'key2' ) ) ) {
$meta_query[] = array( 'key' => 'key2', 'value' => get_query_var( 'key2' ), 'compare' => 'LIKE' );
}
if ( count( $meta_query ) > 1 ) {
$meta_query['relation'] = 'AND';
}
if ( count( $meta_query ) > 0 ) {
$query->set( 'meta_query', $meta_query );
}
}
add_action( 'pre_get_posts', 'yl_custom_product_query' );
But, when I change:
// add meta_query elements
if ( !empty( get_query_var( $subsite ) ) ) {
$meta_query[] = array( 'key' => $subsite, 'value' => get_query_var( 'yes' ), 'compare' => 'LIKE' );
}
to
// add meta_query elements
if ( !empty( $subsite ) ) {
$meta_query[] = array( 'key' => $subsite, 'value' => 'yes', 'compare' => 'LIKE' );
}
(so removing get_query_var twice) the meta_query works great but all static pages suddenly become unreachable...
What am I missing here?

URL Query to filter WooCommerce products by ACF checkbox values?

I need to be able to filter WooCommerce products according to the values from an ACF field with a series of checkboxes e.g. Apples, Bananas, Grapes etc. Ideally I'd like to be able to do this with a URL query e.g. https://example.com/shop/?fruit=apples
I've been trying something along these lines with no success:
function product_initial_filter( $query ) {
if( !is_admin() ) {
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'product' ) {
if( isset($GET['fruit']) ) {
$query->set('meta_value', $GET['fruit']);
$query->set('meta_key', 'fruits');
$query->set('meta_compare', 'IN');
}
}
}
// return
return $query;
}
add_action('pre_get_posts', 'product_initial_filter');
You can pass meta argument in meta_query. and also if you are 'compare' => 'IN', the value must be an array. check the below code.
Using WP pre_get_posts action hook.
function product_initial_filter( $query ) {
if( !is_admin() && $q->is_main_query() ) {
if( $query->get('post_type') == 'product' && isset( $GET['fruit'] ) && $GET['fruit'] != '' ) {
$meta_query = array(
array(
'key' => 'fruits',
'value' => array( $GET['fruit'] ),
'compare' => 'IN',
),
);
$query->set( 'meta_query', $meta_query );
}
}
}
add_action('pre_get_posts', 'product_initial_filter');
OR
Using WC woocommerce_product_query action hook.
function product_initial_filter( $query ) {
if( !is_admin() && $q->is_main_query() ) {
if( $query->get('post_type') == 'product' && isset( $GET['fruit'] ) && $GET['fruit'] != '' ) {
$meta_query = array(
array(
'key' => 'fruits',
'value' => array( $GET['fruit'] ),
'compare' => 'IN',
),
);
$query->set( 'meta_query', $meta_query );
}
}
}
add_action('woocommerce_product_query', 'product_initial_filter');
USEFUL LINKS.
pre_get_posts
woocommerce_product_query
meta_query

Custom WordPress search query ignoring posts_per_page on pre_get_posts

I've been working with this function and for some reason, I cannot seem to figure out the issue here.
I have a function that I want to affect the search results to change the number of posts_per_page of the query to be unlimited.
For whatever reason, $query->set('posts_per_page', -1) and $query->set('post__in', $post__in) appear to do nothing while $query->set('tax_query', $tax_query) does work.
Any ideas would be much appreciated!
function fp_search_query_mod( $query ) {
if ( $query->is_search && $query->is_main_query() && !is_admin() ) :
$cats = ( isset($_GET['cats'] ) ? $_GET['cats'] : null );
$post__in = array();
if ( !empty( $cats ) ) :
$cats = explode( ',', $cats );
$tax_query = array(
'relation' => 'AND',
array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => array( 'simple', 'variable' )
),
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats,
)
);
$query->set( 'tax_query', $tax_query );
if ( in_array( 'favs', $cats ) ) :
$fp_favs = get_user_meta( get_current_user_id(), 'fp_favs', true );
if ( !empty( $fp_favs ) ) {
$post__in = $fp_favs;
}
endif;
if ( in_array( 'purchases', $cats ) ) :
// $purchased = get_customer_purchased_products();
$purchased = get_user_meta( get_current_user_id(), 'fp_purchases', true );
if ( !empty( $post__in ) ) {
$post__in = array_merge( $post__in, $purchased );
$post__in = array_unique( $post__in );
} else {
$post__in = $purchased;
}
endif;
endif;
if ( !empty( $post__in ) ) :
$query->set( 'post__in', $post__in );
endif;
$query->set( 'posts_per_page', -1 );
endif;
}
add_action( 'pre_get_posts', 'fp_search_query_mod', 2 );
Here's the page on which the resuls are displayed: https://filmpacdev.wpengine.com/?s=beaches&post_type=product

Wordpress Remove CPT Slug with hierarchical

I am trying to remove the CPT slug with hierarchical urls:
www.test.com/cpt-slug/post-name/ > www.test.com/post-name
Which I have done fine and it is working with the below code however when I have a children post i.e www.test.com/post-name/child-post-name it doesn't seem to work. I will also need this to work for this type of url too www.test.com/post-name/child-post-name/another-child-post-name
function rewrite_permalink($permalink, $post_id, $leavename) {
$post = get_post($post_id);
if ( self::$post_type != $post->post_type || 'publish' != $post->post_status ) return $permalink;
// Get post
if (!$post) return $permalink;
return strtr($permalink,array('/' . self::$post_type . '/' => '/'));
}
public static function holiday_parse_request( $query ) {
$counts = array( 2, 4 );
if ( ! $query->is_main_query() || !in_array(count( $query->query ), $counts) || ! isset( $query->query['page'] ) ) {
return;
}
$query->set( 'post_type', array( 'post', self::$post_type, 'page' ) );
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', self::$post_type, 'page' ) );
}
}
Thanks in advance.

Wordpress, search in excerpt instead content

im new posting in the forum, but here i found answers without ask, just searching, thanks about that.
Here go my questions:
I have a custom post type called 'publicacion' and there the next custom WP_Query:
$query_args = array(
's' => mysql_real_escape_string( $_GET['s'] ),
'post_type' => 'publicacion',
'meta_key' => 'meta-box-fp'
);
if( $_GET['autor'] != '' ) {
$query_args['tax_query'] = array(
array(
'taxonomy' => 'autores',
'field' => 'slug',
'terms' => mysql_real_escape_string( $_GET['autor'] )
)
);
}
if( trim( $_GET['fini'] ) != '' || trim( $_GET['ffin'] ) != '' ) {
$query_args['meta_query'] = array (
array(
'key' => 'meta-box-fp',
'value' => array( mysql_real_escape_string( $_GET['fini'] ) . '-00-00', mysql_real_escape_string( $_GET['ffin'] ) . '-00-00' ),
'type' => 'DATE',
'compare' => 'BETWEEN' )
);
}
if( isset( $_GET['ord_by'] ) ) {
$ordby = mysql_real_escape_string( $_GET['ord_by'] ) == 'year' ? 'meta_value' : mysql_real_escape_string( $_GET['ord_by'] );
$query_args['orderby'] = $ordby;//mysql_escape_string( $_GET['ord_by'] );
$query_args['order'] = 'DESC';//mysql_escape_string( $_GET['ord'] );
}
$query = new WP_Query( $query_args );
The 's' parameter redirects to the search page, but i need do the search in the archive-publicacion.php file, and i need the 's' parameter, search for excerpt instead content.
It is possible to do that? Thanks and i hope be clear in my question and excuse me my english.
well.. i've resolved my issue by adding the query parameters in functions.php from theme instead from archive.php and changing the 's' to 'str' from the url for not redirect the query when wordpress reads the url.
This looks something like this functions.php:
add_action( 'pre_get_posts', 'my_custom_search' );
function my_custom_search( $query ) {
if( !is_admin() && $query->is_main_query() && $query->get('post_type') == 'publicacion' ) {
if( isset( $_GET['str'] ) && !empty( $_GET['str'] ) ) {
$query->set( 's', mysql_real_escape_string( $_GET['str'] ) );
}
if( ( isset( $_GET['fini'] ) && !empty( $_GET['fini'] ) ) && ( isset( $_GET['ffin'] ) && !empty( $_GET['ffin'] ) ) ) {
$fini = !empty( $_GET['fini'] ) ? mysql_real_escape_string( $_GET['fini'] ) . '-00-00' : '1900-00-00';
$ffin = !empty( $_GET['ffin'] ) ? mysql_real_escape_string( $_GET['ffin'] ) . '-00-00' : date( 'Y' ) . '-00-00';
$args_meta_query = array(
array(
'key' => 'meta-box-fp',
'value' => array( $fini, $ffin ),
'type' => 'DATE',
'compare' => 'BETWEEN'
)
);
$query->set( 'meta_query', $args_meta_query );
}
if( isset( $_GET['autor'] ) && !empty( $_GET['autor'] ) ) {
$args_tax_query = array(
array(
'taxonomy' => 'autores',
'field' => 'slug',
'terms' => mysql_real_escape_string( $_GET['autor'] )
)
);
$query->set( 'tax_query', $args_tax_query );
}
if( isset( $_GET['ordby'] ) && $_GET['ordby'] != 'fecha' ) {
$ordby = mysql_real_escape_string( $_GET['ordby'] );
$query->set( 'orderby', $ordby );
$query->set( 'order', strtoupper( mysql_real_escape_string( $_GET['ord'] ) ) );
}else {
$ord = isset( $_GET['ord'] ) && !empty( $_GET['ord'] ) ? strtoupper( mysql_real_escape_string( $_GET['ord'] ) ) : 'DESC';
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'meta_key', 'meta-box-fp' );
$query->set( 'order', $ord );
}
}
}
I hope this help somebody and excuse me my english again.
Bye.

Categories