WP Grid Builder populate grid from custom field (ACF gallery) - php

I would like to populate WPGRIDBUILDER grid from ACF Gallery field.
Support told me:
Grids only work with WOrdPress content type: (custom) post types, (custom) taxonomy terms and WordPress users.
So, what you are looking for requires to use custom PHP code in order to populate a grid from ACF field.
And the field must hold image IDs uploaded in WordPress media library.
Here are two snippets which I assume can be the answer. I'm totally newbie in PHP.
https://docs.wpgridbuilder.com/resources/filter-grid-query-args/
function prefix_query_args( $query_args, $grid_id ) {
if ( 1 === $grid_id ) {
$query_args['post__not_in'] = [ 1, 2, 3, 4 ];
}
return $query_args;
}
add_filter( 'wp_grid_builder/grid/query_args', 'prefix_query_args', 10, 2 );
https://support.advancedcustomfields.com/forums/topic/gallery-field-return-images-ids/
$images = get_field('gallery_field'); //gallery field
foreach( $images as $img ):
$img_srcset = wp_get_attachment_image_srcset($img['ID']); // return Image ID for each in gallery
Here is my take on it:
function prefix_query_args( $query_args, $grid_id ) {
if ( 2 === $grid_id ) { //mygrid id
$query_args['post_type'] = [ 'realizacje-cpt' ];//my cpt
$images = get_field('gallery-cf'); //gallery field
foreach( $images as $img )
$img_srcset = wp_get_attachment_image_srcset($img['ID']);
}
return $query_args,$images;
}
add_filter( 'wp_grid_builder/grid/query_args', 'prefix_query_args', 10, 2 );
Em I even going in right direction?

Related

How to pre-filter Woocommerce by current category name?

I'm trying pre-filter categories on product archive pages by the current category name using the WP Grid Builder plugin. The idea is to use a single grid to show related products. I have this code here:
function prefix_filter_query( $query_string, $grid_id, $action ) {
// If the content is not filtered on first render.
if ( 'render' === $action && empty( $query_string ) && 1 === $grid_id ) {
$query_string = [
'categories' => ['home-decor']
];
}
return $query_string;
}
add_filter( 'wp_grid_builder/facet/query_string', 'prefix_filter_query', 10, 3 );
It works fine, but I have to create a new grid and hook it for every category. I'd rather have "['home-decor']" be dynamic.
In my head it works like this:
myterm = current_product_category;
$query_string = [
'categories' => myterm
How can I do this? Thank you.

How to filter taxonomy terms in WP Admin?

I'm trying to filter a custom taxonomy in the backend of my wordpress site to allow certain users to use only some of the available entries.
Through the Advanced Custom Fields plugin I have created a custom metabox in the user panel that allows me to disable or enable at will a list of terms (WP_Terms) among those existing in my custom taxonomy.
But now I need to show only the terms selected from the user page in the WordPress post editor.
This is the code I have come up with so far but it seems that I have run aground.
function filter_admin_allowed_regions ( $terms, $id, $taxonomy ) {
$terms = wp_cache_get( $id, "{$taxonomy}_relationships_sorted" );
$newTerms = array();
if(is_admin())
if ( $taxonomy->name == "regioni" ) {
$allowed = the_field('tassonomie_abilitate', 'user_'.get_current_user_id());
return $allowed;
}
return $terms;
}
add_filter( 'get_the_terms', 'filter_admin_allowed_regions' , 10, 4 );

Moving custom field images to product Gallery Woocommerce

I've several custom image fields (ACF) from an old configuration, and would like to move those images in the Product Gallery (Woocommerce), now I've converted all the datas into a Product post type.
I tried to set this function (found in a similar post), but nothing happens, and no errors returned neither :
function upload_all_images_to_product($product_id, $image_id_array) {
//define the array with custom fields images
$image_1 = get_field('images'); // should returns image IDs
$image_2 = get_field('images-2');
$image_3 = get_field('images-3');
$image_4 = get_field('images-4');
$image_5 = get_field('images-5');
$image_6 = get_field('images-6');
$image_id_array = array($image_1, $image_2, $image_3, $image_4, $image_5, $image_6);
//take the first image in the array and set that as the featured image
set_post_thumbnail($product_id, $image_id_array[0]);
//if there is more than 1 image - add the rest to product gallery
if(sizeof($image_id_array) > 1) {
array_shift($image_id_array); //removes first item of the array (because it's been set as the featured image already)
update_post_meta($product_id, '_product_image_gallery', implode(',',$image_id_array)); //set the images id's left over after the array shift as the gallery images
}
}
Could someone help or explain me what's wrong ?
Depending on where you run this function, you should define the $product_id argument in ACF get_field() function.
Questions to clarify: How do you run this function? are you using a hook?
Update: Hooked the function in woocommerce_process_product_meta, so when a product is created or updated, it will trigger the code.
Also your code can be simplified, optimized and compacted as follow:
add_action( 'woocommerce_process_product_meta', 'save_my_custom_settings' );
function upload_all_images_to_product( $product_id, $image_ids = array(); ) {
// Loop from 1 to 6
for ( $i = 1; $i <= 6; $i++ ) {
$field_key = 'images'.( $i == 1 ? '' : '-'.$i );
// Check that the custom field exists
if( $field_value = get_field( $field_key, $product_id ) )
$image_ids[] = $field_value; // Set each ACF field value in the array
}
if( ! empty($image_ids) ) {
// Take the first image (removing it from the array) and set it as the featured image
set_post_thumbnail( $product_id, array_shift($image_ids) );
}
if( ! empty($image_ids) ) {
// Set the remaining array images ids as a coma separated string for gallery images
update_post_meta( $product_id, '_product_image_gallery', implode(',', $image_ids) );
}
}
Code goes in functions.php file of your active child theme (or active theme). untested it could work.

Remove Wordpress Plugin functionality from admin side

I'm using a WordPress plugin https://github.com/lesterchan/wp-postratings.
It also showing ratings on admin, when i visit http://domain.com/wp-admin/edit.php.
How do i remove those ratings from admin side.
You can use the below function in your functions.php file with the manage_posts_columns filter. I'm assuming your custom post type id 'tools' and the column is referenced by 'ratings'. If they are different you can just change that in the code.
add_filter( 'manage_posts_columns', 'custom_post_columns', 10, 2 );
function custom_post_columns( $columns, $post_type ) {
switch ( $post_type ) {
//assuming the id for the custom post type is 'tools'
case 'tools':
unset(
//I'm using 'ratings' but you'll have to check and see what the name for the column really is.
$columns['ratings']
);
break;
}
return $columns;
}

pull out data from database and display in Gravity form

I am using gravity forms plugin, and I'm trying to display the categories as a drop down list in the form I have already created.
If required, please here's a link to my website
I've been on this for too long, and no way out. Kindly help me out.
add_filter( 'gform_pre_render_1', 'populate_categories' );
add_filter( 'gform_pre_validation_1', 'populate_categories' );
add_filter( 'gform_pre_submission_filter_1', 'populate_categories' );
add_filter( 'gform_admin_pre_render_1', 'populate_categories' );
function populate_categories( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->id != 1 ) {
continue;
}
// you can add additional parameters here to alter the posts that are retrieved
// more info: [http://codex.wordpress.org/Template_Tags/get_posts](http://codex.wordpress.org/Template_Tags/get_posts)
$categories = get_categories ;
$choices = array();
foreach ( $categories as $categories ) {
$choices[] = array( 'text' => $categories->name, 'value' => $categories->name );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Category';
$field->choices = $choices;
}
return $form;
}
You can dynamically generate drop downs for gravity forms using the syntax provided below in this link. You have to take control over the functions.php file of the theme to retrieve your output as per your requirement.
Here is the clear documentation provided and you can create in a simple manner using this methods. There are two methods available refer to it.
https://www.gravityhelp.com/documentation/article/dynamically-populating-drop-down-fields/
If you face any problem with creation let me know we shall solve it.

Categories