Why attribute from shortcode doesn't work? - wordpress

I have such code with attribute extraction to use it in shortcode.
<?php
function wpusers_shortcode( $atts ) {
extract(shortcode_atts( array(
'Department' => '',
), $atts
)
);
$args = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'Department',
'value' => $Department,
),
)
);
// The Query
$user_query = new WP_user_Query( $args );
// User Loop
if ( ! empty( $user_query->get_results() ) ) {
foreach ( $user_query->get_results() as $user ) {
echo '<p>' . $user->display_name . '</p>';
}
} else {
echo 'No users found.';
}
}
add_shortcode( 'wpusers', 'wpusers_shortcode' );
?>
The shortcode is [wpusers Department="IEDK"].
Users are assigned to custom field Department
enter image description here
But at front end I have a message - No Users Found.
When I add attr (IEDK) in the code I can see them
extract(shortcode_atts( array(
'Department' => 'IEDK',
or
'key' => 'Department',
'value' => 'IEDK',
Whhere can be a problem?
Thanks in advance.

Remove extract and then try to debug with uncomment lines
function wpusers_shortcode( $atts ) {
$ar = shortcode_atts(
array('Department' => ''),
$atts
);
// print_r($ar); die;
$args = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'Department',
'value' => $ar['Department'],
),
)
);
// print_r($args); die;
$user_query = new WP_user_Query( $args );
if ( ! empty( $user_query->get_results() ) ) {
foreach ( $user_query->get_results() as $user ) {
echo '<p>' . $user->display_name . '</p>';
}
} else {
echo 'No users found.';
}
}
add_shortcode( 'wpusers', 'wpusers_shortcode' );

Solved.
The problem was in Uppercase of key [wpusers Department="IEDK"].
I changed to small "d" everywhere and now it works..........

Related

WooCommerce: disable AJAX reload on product page when selecting an attribute

in WooCommerce 4.2.0, when the user selects an attribute on the product page (i.e. product size), the AJAX reloads the "other products" in WooCommerce tabs. It's painful for the user to wait for this. How to disable this AJAX reload please ?
The code for the "other colors" tabs in my Storefront child-theme's functions.php:
/*ADD custom "other colors" tab */
add_filter( 'woocommerce_product_tabs', 'wpb_new_product_tab_othercolors' );
function wpb_new_product_tab_othercolors( $tabs ) {
// Add the new tab
global $post;
$othercolorsbool = get_post_meta( $post->ID, '_othercolorsbool', true );
// var_dump('$othercolorsbool :');
// var_dump($othercolorsbool);
if ( $othercolorsbool === "false" || $othercolorsbool === "" || $othercolorsbool === "0" ) {
unset( $tabs['othercolors_tab'] );
return $tabs;
} else {
if( get_locale() == 'fr_FR' ) {
$tabs['othercolors_tab'] = array(
'title' => __( 'AUTRES COULEURS', 'text-domain' ),
'priority' => 0,
'callback' => 'wpb_new_product_tab_content_othercolors'
);
} else {
$tabs['othercolors_tab'] = array(
'title' => __( 'OTHER COLORS', 'text-domain' ),
'priority' => 0,
'callback' => 'wpb_new_product_tab_content_othercolors'
);
}
return $tabs;
}
}
function wpb_new_product_tab_content_othercolors() {
global $post;
$title = $post->post_title;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
break;
}
$splitted = product_name_split($title);
$productname = $splitted[0];
$productcolor = $splitted[1];
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $product_cat_id
),
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $product_cat_id
)
),
'post_type' => 'product',
'orderby' => 'title',
);
$the_query = new WP_Query( $args );
// The Loop
$r = 0;
while ( $the_query->have_posts() ) {
$r = $r + 1;
$the_query->the_post();
$currenttitle = get_the_title();
if ( strpos($currenttitle, $productname) !== false) { //if iteration product has the same name as the productname, then find its color: $productcolorbis
$splitted = product_name_split($currenttitle);
$productnamebis = $splitted[0];
$productcolorbis = $splitted[1];
if ( ($productcolorbis !== $productcolor) ) { //if iteration product color is different than productcolor, then go
wc_get_template_part( 'content', 'product' );
}
}
}
wp_reset_postdata();
}

Create a page with child pages and have each child have it's own page template

I have some code that creates pages with child pages in WordPress. What i would like to do is have each child have its own page template.
As you see below, i am creating child pages of Script and Assets. Currently the child page is created with one page_Full.php added to everything but i would like to have Script and Assets have their own page template. Ideas would be helpful.
Thanks in advance.
function CreatePage(){
$post_title = $_POST['ProjectName'];
$post_excerpt = $_POST['ProjectDiscript'];
$tags = $_POST['TypeOption'];
$pages = array(
array(
'name' => $post_title,
'title' => $post_title,
'child' => array(
'script' => $post_title.'_Script',
'assets' => $post_title.'_Assets'
)
)
);
$template = array(
'post_type' => 'page',
'post_status' => 'publish',
'post_author' => $user_id,
'post_excerpt' => $post_excerpt,
'tags_input' => array($tags),
);
foreach( $pages as $page ) {
$exists = get_page_by_title( $page['title'] );
$my_page = array(
'post_name' => $page['name'],
'post_title' => $page['title'],
'page_template' => 'page_Full.php'
);
$my_page = array_merge( $my_page, $template );
$id = ( $exists ? $exists->ID : wp_insert_post( $my_page ) );
if( isset( $page['child'] ) ) {
foreach( $page['child'] as $key => $value ) {
$child_id = get_page_by_title( $value );
$child_page = array(
'post_name' => $key,
'post_title' => $value,
'post_parent' => $id,
'page_template' => 'page_Full.php'
);
$child_page = array_merge( $child_page, $template );
if( !isset( $child_id ) ) wp_insert_post( $child_page );
}
}
}
wp_die();
}
add_action( 'wp_ajax_CreatePage','CreatePage' );
Had the idea to create an array for every child page - and it seams to work. Perhaps there is still a better way but for the time being - This does the trick!
$pages = array(
array(
'name' => $post_title,
'title' => $post_title,
'child' => array(
'script' => array(
'Pname' => $post_title.'_Script',
'Ptemplate' => 'page_Full_Script.php'
),
'assets' => array(
'Pname' => $post_title.'_Assets',
'Ptemplate' => 'page_Full_Assets.php'
),
'settings' => array(
'Pname' => $post_title.'_Settings',
'Ptemplate' => 'page_Full_Settings.php'
)
)
)
);
Here is the child with sub arrays.
if( isset( $page['child'] ) ) {
foreach( $page['child'] as $key[] => $value ) {
$child_id = get_page_by_title( $value );
$child_page = array(
'post_name' => $value['Pname'],
'post_title' => $value['Pname'],
'post_parent' => $id,
'page_template' => $value['Ptemplate'],
);
$child_page = array_merge( $child_page, $template );
if( !isset( $child_id ) ) wp_insert_post( $child_page );
}
}
And here is where the child array is set as var to be iterated through so that the info can be inserted into the child_page var to be merged and the post inserted.
Thanks to any who took the time to review.
Cheers

Issue with function and post ID

I am using the following function/shortcode in order to output an average global rating using ACF :
function get_average_rating($post_id) {
$rating_sum = 0;
$reviews_of_post = get_posts( array(
'post_type' => 'avis',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'produit',
'value' => $post_id,
'compare' => '=',
),
),
) );
if ( empty( $reviews_of_post ) ) {
return 0;
}
foreach ( $reviews_of_post as $review ) {
$rating_sum += get_field( 'note_client', 'post_' . $review->ID);
}
return number_format((float)($rating_sum / count( $reviews_of_post )),1, ',', '');
}
add_shortcode( 'note-clients', 'get_average_rating');
It always return 0 except when I manually input the post ID like :
'meta_query' => array(
array(
'key' => 'produit',
'value' => 1234,
'compare' => '=',
),
),
How can I fix this ?
Thanks a lot !
Declare global $post; before your function.
Wordpress uses $post for many of its functions within the loop.
To avoid any conflicts later you should consider use of wp_reset_query
1 . Change key 'key' => 'produit' to 'key' => 'product',
2 .
foreach ( $reviews_of_post as $review ) {
$rating_sum += get_field( 'note_client', 'post_' . $review->ID);
}
**to**
foreach ( $reviews_of_post as $review ) {
$post_id = 'post_' . $review->ID ;
$rating_sum += get_field( 'note_client', $post_id );
}
Confirm ACF key (note_client) is correct
3 . Change this
add_shortcode( 'note-clients', 'get_average_rating');
to
add_shortcode( 'average_rating', 'get_average_rating');
Please try with your post static id:
$post_id = 9; //add here your static post id
$reviews_of_post = get_posts( array(
'post_type' => 'avis',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'produit',
'value' => $post_id,
'compare' => '=',
),
),
) );

How to get the first topic in a lesson in learndash

I want to retrieve the first topic in a lesson in learndash and redirect it through it. But for some reason am not sure how to do it.
I checked the API and doesn't see any appropriate filter/hook for it.
Here is my code:
function redirect_to_first_topic() {
// We only want to do this for Topics. But the below code can be adapted to work for Lessons
global $post;
$post_id = $post->ID;
if ( get_post_type( $post_id ) == 'sfwd-lessons' ) {
$progress = learndash_get_course_progress( null, $post_id );
$link = $progress['next'];
$parent_lesson_id = learndash_get_setting( $post, 'topic' );
$parent_lesson = get_post( $parent_lesson_id );
var_dump($parent_lesson);
}
// Always return $link
// return $link;
}
add_action('wp', 'redirect_to_first_topic');
So basically what am doing here is getting the parent which is the lesson.
A bit late, but mb it will help someone else. You can see how to get 1. lesson of a course and go deeper if needed fe. first topic of first lesson and so on :
add_action( 'template_redirect', 'course_overview_redirect' );
function course_overview_redirect() {
global $post;
if ( ! empty( $post ) && $post->post_type == 'sfwd-courses' && ! empty( $post->ID ) ) {
$lessons = learndash_get_course_lessons_list( $post->ID );
if ( ! empty( $lessons ) ) {
$lesson = array_shift( $lessons );
if ( ! empty( $lesson ) ) {
$url = get_permalink( $lesson[ "post" ]->ID );
if ( ! empty( $url ) ) {
wp_redirect( esc_url( $url ) ); // And redirect if needed
exit;
}
}
}
}
}
You can add a button like this;
$args = array(
'posts_per_page' => -1,
'post_type' => 'sfwd-lessons', // you can change here to find topics : 'sfwd-topics'
'suppress_filters' => true,
'fields' => 'ids',
'orderby' => 'menu_order', // ordering by menu_order will show lesson list, in their order in course.
'order' => 'ASC',
// this meta query is used if you want to search a lesson under a course, or if you search for a topic which is in a course but not under a lesson
'meta_query' => array(
array(
'key' => 'course_id',
'value' => $course_id, // this is id of your course
)
// if your topic is under a lesson than you should add lesson meta query
// your meta query should be changed like below
/*
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'course_id',
'value' => $course_id,
),
array(
'key' => 'lesson_id',
'value' => $lesson_id,
),
),
*/
),
);
$all_lessons = get_posts($args);
$first_lesson = $all_lessons[0]; // taking directly first element of array
$button_html = '' . __( 'Start the course', 'custom-text' ) . '';

Query metadata to display the orders. Woocommerce

I am trying to display the orders using the query->set() with my customer metadata. The metadata - _admin_name is stored in the wp_postmeta, and I want the admins only can view their own orders which the admin_name same with their user name.
How how can I use query->set() with my metadata?
Here is my code:
function mypo_parse_query_useronly( $wp_query ) {
global $post;
if ( $wp_query->is_admin && strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php?post_type=shop_order') == true) {
add_action( 'views_edit-post', 'child_remove_some_post_views' );
global $current_user;
$userName = $current_user->user_login;
$meta_query_args = array(
array(
'key' => '_user_name',
'value' => $userName,
'compare' => '='
)
);
//trying use this code
// $query = new WP_Query( $meta_query_args );
$wp_query->set( 'author', $current_user->id );
}
}
add_filter('parse_query', 'mypo_parse_query_useronly' );
//don't display the order whish is not own by current user.
function child_remove_some_post_views( $views ) {
//header('Location: '.$newURL);
unset($views['all']);
unset($views['wc-processing']);
unset($views['wc-on-hold']);
unset($views['wc-completed']);
unset($views['pending']);
return $views;
}
Have so way to do this?
Thanks.
<?php
function mypo_parse_query_useronly( $wp_query ) {
global $post;
if ( $wp_query->is_admin && strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php?post_type=shop_order') == true) {
add_action( 'views_edit-post', 'child_remove_some_post_views' );
global $current_user;
$userName = $current_user->user_login;
$meta_query_args = array(
array(
'key' => '_user_name',
'value' => $userName,
'compare' => '='
)
);
//trying use this code
// $query = new WP_Query( $meta_query_args );
$wp_query->set( 'author', $current_user->id );
$wp_query->set( 'meta_query', array(
array(
'key' => '_user_name',
'value' => $userName,
'compare' => '='
)
));
}
}
add_filter('parse_query', 'mypo_parse_query_useronly' );
//don't display the order whish is not own by current user.
function child_remove_some_post_views( $views ) {
//header('Location: '.$newURL);
unset($views['all']);
unset($views['wc-processing']);
unset($views['wc-on-hold']);
unset($views['wc-completed']);
unset($views['pending']);
return $views;
}

Resources