Last active
January 6, 2022 23:46
-
-
Save willybahuaud/522876d7262524a8b71aae402b316d19 to your computer and use it in GitHub Desktop.
filter wpastra custom layout conditions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'astra_display_on_list', 'w_astra_display_on_list_pro' ); | |
function w_astra_display_on_list_pro( $rules ) { | |
// look at $rules array to see where you should add your filter | |
$rules['basic']['value']['pro_only'] = 'Pages professionnels'; | |
return $rules; | |
} | |
function my_condition_is_ok_here( $id ) { | |
// here use conditionnal tags to check if your current page match your filter criterae | |
return is_singular() && 'yes' == get_post_meta( $id, 'is_pro', true ); | |
} | |
add_filter( 'astra_meta_args_post_by_condition', 'w_get_location_metadata', 10, 3 ); | |
function w_get_location_metadata( $meta_args, $q_obj, $current_post_id ) { | |
if ( my_condition_is_ok_here( $current_post_id ) ) { | |
// if condition ok, return layout that match filter name | |
$meta_args .= ' OR pm.meta_value LIKE \'%"pro_only"%\''; | |
} | |
return $meta_args; | |
} | |
/** | |
* Exclusive filter are check before layouts loading, checking exlusive filters for each one | |
*/ | |
add_filter( 'astra_get_display_posts_by_conditions', 'w_astra_get_display_posts_by_conditions', 10, 2 ); | |
function w_astra_get_display_posts_by_conditions( $data, $type ) { | |
if ( 'astra-advanced-hook' == $type ) { | |
foreach ( $data as $k => $d ) { | |
// here is the meta name cheked for exclusive filters | |
$m = get_post_meta( $d['id'], 'ast-advanced-hook-exclusion', true ); | |
if ( ! empty( $m ) ) { | |
if ( in_array( 'pro_only', $m['rule'] ) ) { | |
if ( my_condition_is_ok_here( get_queried_object_id() ) ) { | |
unset( $data[ $d['id'] ] ); | |
} | |
} | |
} | |
} | |
} | |
return $data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment