I'm trying to populate a checkbox ACF field with the various post types of a WP site. This is for a plugin, so the post types will vary based on the location of install.
By default, the plugin uses pages and posts as it's post types, but need to give user option to use checkboxes to select other CPT's on the site. How can I populate the checkbox field with the list of all CPT's on a site. Here is my current section of PHP code to load the field within the plugin
array (
'key' => 'field_56e6d87b6c7be',
'label' => 'Add to Custom Post Types',
'name' => 'fb_pixel_cpt_select',
'type' => 'checkbox',
'instructions' => 'Select which Custom Post Types you would like to use.',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'choices' => array (
),
'default_value' => array (
),
'layout' => 'vertical',
'toggle' => 0,
),
You could use the ACF load field function here to auto populate your field. See more here: http://www.advancedcustomfields.com/resources/acfload_field/
Then, using the Wordpress get_post_types call (https://codex.wordpress.org/Function_Reference/get_post_types) you could retrieve those values and populate your field like this.
add_filter('acf/load_field/name=fb_pixel_cpt_select', 'acf_load_post_types');
function acf_load_post_types($field)
{
foreach ( get_post_types( '', 'names' ) as $post_type ) {
$field['choices'][$post_type] = $post_type;
}
// return the field
return $field;
}
Make sure that your field is already created before you try to populate it though.
This will create a select field for your ACF block or whatever that has all the post types except those that don't have a nav menu (such as the attachment post type).
add_filter('acf/load_field/name=select_post_type', 'yourprefix_acf_load_post_types');
/*
* Load Select Field `select_post_type` populated with the value and labels of the singular
* name of all public post types
*/
function yourprefix_acf_load_post_types( $field ) {
$choices = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
foreach ( $choices as $post_type ) :
$field['choices'][$post_type->name] = $post_type->labels->singular_name;
endforeach;
return $field;
}
When I need to do this (choose from list of post types) I would be creating a list of posts based on post-type to feed into WP Query. So I use a select field in ACF, and add the post type names into the select options when creating the field, e.g.
posts : Posts
which I then use in the arguments for the WP Query, like so:
$theselectfield = get_field('theselectfield');
Then use it in the arguement of the query:
$args = array(
'post_type' => $theselectfield,
'some_other' => 'argument',
);
Related
My goal is to get a signed-in user to select a color( via front end form and saved in ACF user meta field group) that will be applied to another user meta field inside a repeater. The field must be the same for each row inside the repeater ( for front-end design reasons ). I am using ACF pro, and a plugin called ACF Front end admin (let's call it FEA from now on) for the front-end form. I'm pretty new to PHP and have referenced ACF & FEA's s documentation, which can be found below, spin up a custom function. I am currently running this through the code snippets plugin if this helps. I've run an error log and nothing related to this shows. Tried running this through wp-config and it crashes my site.
Front end admin documentation
ACF documentation - update sub field
ACF documentation - getting values from a user
Edit, Additional information:
ACF Field composition of 'button color' (color of the buttons that user wants displayed in front end, and saved as user meta):
acf_add_local_field_group(array(
'key' => 'group_60000aaa00000',
'title' => 'Color Selector',
'fields' => array(
array(
'key' => 'field_60000aaa00000',
'label' => 'Button color',
'name' => 'button_color',
'type' => 'color_picker',
Button color in repeater field that needs to updated based on the above mentioned 'button_color' meta value:
acf_add_local_field_group(array(
'key' => 'group_70000bbb00000',
'title' => 'front end user profile',
'fields' => array(
array(
'key' => 'field_70000bbb00000',
'label' => 'Quick Link selector',
'name' => 'quick_link_selector',
'type' => 'repeater',
),
array(
'key' => 'field_70000ccc00000',
'label' => 'repeater button color',
'name' => 'repeater_button_color',
'type' => 'text',
The title of the forum is: "QL color selector form" (created through the FEA plugin) and has a short code of [frontend_admin form="3030"] if this helps.
I'm running the following code with no luck and would really appreciate any help!
/// Hooks into any(?) form and does something
add_action('acf_frontend/save_user', 'retrieve_colors_2_update', 10, 2);
function retrieve_colors_2_update( $form, $user_id ) {
$user = get_user_by('ID',$user_id);
//get important fields
$btn_color = get_field('button_color', 'user_' .$user_id);
//// update a repeater loop (ACF)
if( have_rows('quick_link_selector', 'user_'.$user_id) ) {
$i = 0;
while( have_rows('quick_link_selector') ) {
the_row();
$i++;
update_sub_field('repeater_button_color', $btn_color );
}
}
}
I want to filter list that does not have an invalid email and opt-out email in Leads and Contact module using SugarCRM 7 API.
I have added below email filter in arguments but does not work. How to email filter via SugarCRM 7.x rest API.
$filter_arguments = array(
"filter" => array(
array(
"assigned_user_id" => 1,
),
array(
"email1" => array(
array(
'opt_out' => array(
'$equals' => ''
)
)
)
),
),
);
$url = $base_url . "/Contacts/filter";
Thanks.
This much code will not help you please check this :
Reference Link 1
Reference Link 2
The following example will demonstrate how to add a predefined filter on the Accounts module to return all records with an account type of "Customer" and industry of "Other".
To create a predefined filter, create a display label extension in ./custom/Extension/modules/<module>/Ext/Language/. For this example, we will create:
./custom/Extension/modules/Accounts/Ext/Language/en_us.filterAccountByTypeAndIndustry.php
<?php
$mod_strings['LBL_FILTER_ACCOUNT_BY_TYPE_AND_INDUSTRY'] = 'Customer/Other Accounts';
Next, create a custom filter extension in ./custom/Extension/modules/<module>/Ext/clients/base/filters/basic/.
For this example, we will create:
./custom/Extension/modules/Accounts/Ext/clients/base/filters/basic/filterAccountByTypeAndIndustry.php
<?php
$viewdefs['Accounts']['base']['filter']['basic']['filters'][] = array(
'id' => 'filterAccountByTypeAndIndustry',
'name' => 'LBL_FILTER_ACCOUNT_BY_TYPE_AND_INDUSTRY',
'filter_definition' => array(
array(
'account_type' => array(
'$in' => array(
'Customer',
),
),
),
array(
'industry' => array(
'$in' => array(
'Other',
),
),
),
),
'editable' => false,
'is_template' => false,
);
You should notice that the editable and is_template options have been set to "false". If editable is not set to "false", the filter will not be displayed in the list view filter's list.
Finally, navigate to Admin > Repair and click "Quick Repair and Rebuild" to rebuild the extensions and make the predefined filter available for users.
Adding Initial Filters to Lookup Searches
To add initial filters to record lookups and type-ahead searches, define a filter template. This will allow you to filter results for users when looking up a parent related record. The following example will demonstrate how to add an initial filter for the Account lookup on the Contacts module. This initial filter will limit records to having an account type of "Customer" and a dynamically assigned user value determined by the contact's assigned user.
To add an initial filter to the Contacts record view, create a display label for the filter in ./custom/Extension/modules/<module>/Ext/Language/. For this example , we will create:
./custom/Extension/modules/Accounts/Ext/Language/en_us.filterAccountTemplate.php
<?php
$mod_strings['LBL_FILTER_ACCOUNT_TEMPLATE'] = 'Customer Accounts By A Dynamic User';
Next, create a custom template filter extension in ./custom/Extension/modules/<module>/Ext/clients/base/filters/basic/.
For this example, create:
./custom/Extension/modules/Accounts/Ext/clients/base/filters/basic/filterAccountTemplate.php
<?php
$viewdefs['Accounts']['base']['filter']['basic']['filters'][] = array(
'id' => 'filterAccountTemplate',
'name' => 'LBL_FILTER_ACCOUNT_TEMPLATE',
'filter_definition' => array(
array(
'account_type' => array(
'$in' => array(),
),
),
array(
'assigned_user_id' => ''
)
),
'editable' => true,
'is_template' => true,
);
As you can see, the filter_definition contains arrays for account_type and assigned_user_id. These filter definitions will receive their values from the contact record view's metadata. You should also note that this filter has is_template and editable set to "true". This is required for initial filters.
Once the filter template is in place, modify the contact record view's metadata. To accomplish this, edit ./custom/modules/Contacts/clients/base/views/record/record.php to adjust the account_name field. If this file does not exist in your local Sugar installation, navigate to Admin > Studio > Contacts > Layouts > Record View and click "Save & Deploy" to generate it. In this file, identify the panel_body array as shown below:
1 =>
array (
'name' => 'panel_body',
'label' => 'LBL_RECORD_BODY',
'columns' => 2,
'labelsOnTop' => true,
'placeholders' => true,
'newTab' => false,
'panelDefault' => 'expanded',
'fields' =>
array (
0 => 'title',
1 => 'phone_mobile',
2 => 'department',
3 => 'do_not_call',
4 => 'account_name',
5 => 'email',
),
),
Next, modify the account_name field to contain the initial filter parameters.
1 =>
array (
'name' => 'panel_body',
'label' => 'LBL_RECORD_BODY',
'columns' => 2,
'labelsOnTop' => true,
'placeholders' => true,
'newTab' => false,
'panelDefault' => 'expanded',
'fields' =>
array (
0 => 'title',
1 => 'phone_mobile',
2 => 'department',
3 => 'do_not_call',
4 => array (
//field name
'name' => 'account_name',
//the name of the filter template
'initial_filter' => 'filterAccountTemplate',
//the display label for users
'initial_filter_label' => 'LBL_FILTER_ACCOUNT_TEMPLATE',
//the hardcoded filters to pass to the templates filter definition
'filter_populate' => array(
'account_type' => array('Customer')
),
//the dynamic filters to pass to the templates filter definition
//please note the index of the array will be for the field the data is being pulled from
'filter_relate' => array(
//'field_to_pull_data_from' => 'field_to_populate_data_to'
'assigned_user_id' => 'assigned_user_id',
)
),
5 => 'email',
),
),
Finally, navigate to Admin > Repair and click "Quick Repair and Rebuild". This will rebuild the extensions and make the initial filter available for users when selecting a parent account for a contact.
Adding Initial Filters to Drawers from a Controller
When creating your own views, you may need to filter a drawer called from within your custom controller. Using an initial filter, as described in the Adding Initial Filters to Lookup Searches section, we can filter a drawer with predefined values by creating a filter object and populating the config.filter_populate property as shown below:
//create filter
var filterOptions = new app.utils.FilterOptions()
.config({
'initial_filter': 'filterAccountTemplate',
'initial_filter_label': 'LBL_FILTER_ACCOUNT_TEMPLATE',
'filter_populate': {
'account_type': ['Customer'],
'assigned_user_id': 'seed_sally_id'
}
})
.format();
//open drawer
app.drawer.open({
layout: 'selection-list',
context: {
module: 'Accounts',
filterOptions: filterOptions,
parent: this.context
}
});
To create a filtered drawer with dynamic values, create a filter object and populate the config.filter_relate property using the populateRelate method as shown below:
//record to filter related fields by
var contact = app.data.createBean('Contacts', {
'first_name': 'John',
'last_name': 'Smith',
'assigned_user_id': 'seed_sally_id'
});
//create filter
var filterOptions = new app.utils.FilterOptions()
.config({
'initial_filter': 'filterAccountTemplate',
'initial_filter_label': 'LBL_FILTER_ACCOUNT_TEMPLATE',
'filter_populate': {
'account_type': ['Customer'],
},
'filter_relate': {
'assigned_user_id': 'assigned_user_id'
}
})
.populateRelate(contact)
.format();
//open drawer
app.drawer.open({
layout: 'selection-list',
context: {
module: 'Accounts',
filterOptions: filterOptions,
parent: this.context
}
});
here my comma separated column is shops(eg. 1,2,3,4), and with the call back i am already displaying the multiple shops names. with call back i am showing the related shop-names(values shows eg. Shop1,Shop2,Shop3,Shop4).
is there a way i can filter it with the values i am displaying.
$this->fields_list = array(
'id_push' => array('title' => $this->l('ID')),
'shops' => array('title' => $this->l('Shop(s)'),'callback' => 'getShopName','type'=>'editable')
);
You should include a concatenated shop name field in your controller SELECT. Then you should specify filter_key parameter in your shops fieldlist field. Something like this:
$this->_select = ' a.`correct_field_name` AS `shopnames_custom_field`';
$this->fields_list = array(
'id_push' => array('title' => $this->l('ID')),
'shops' => array('title' => $this->l('Shop(s)'),'callback' => 'getShopName','type'=>'editable', 'filter_key' => 'shopnames_custom_field')
);
If this solution does not work you should modify getList function to custom filter results.
Good luck
Right, I've got WordPress E-commerce installed on WordPress and I need to add additional columns to the post type.
I've done some investigating. It appears that E-commerce just submits a post type called "Products" and changes the columns in order to add things like Price etc.
I need to add another input. Just a little checkbox that the admin can set to true or false as they add a product. The only problem for me at the moment is finding where exactly to do this.
I think I've found the WordPress E-Commerce post type column settings, but obviously just adding an additional one isn't working.
/wp-content/plugins/wp-e-commerce/wpsc-admin/display-items.page.php
function wpsc_additional_column_names( $columns ){
$columns = array();
$columns['cb'] = '';
$columns['image'] = '';
$columns['title'] = __('Name', 'wpsc');
$columns['stock'] = __('Stock', 'wpsc');
$columns['price'] = __('Price', 'wpsc');
$columns['sale_price'] = __('Sale', 'wpsc');
$columns['SKU'] = __('SKU', 'wpsc');
$columns['weight'] = __('Weight', 'wpsc');
$columns['cats'] = __('Categories', 'wpsc');
$columns['featured'] = '';
$columns['hidden_alerts'] = '';
$columns['date'] = __('Date', 'wpsc');
return $columns;
}
Don't edit the core files. You can add custom metaboxes to WP e-Commerce's Products post type just as you would any other post type.
My preferred solution is to use Custom Metaboxes and Fields for WordPress
This sample function will output a checkbox on products using the above plugin (note 'pages' => array('wpsc-product'), this targets products only):
function base_meta_boxes_ba($meta_boxes) {
/**
* Page Options meta box
*/
$meta_boxes[] = array(
'id' => 'product_options',
'title' => 'Extra Product Options',
'pages' => array('wpsc-product'),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
'fields' => array(
array(
'name' => 'Test Checkbox',
'desc' => 'field description (optional)',
'id' => $prefix . 'test_checkbox',
'type' => 'checkbox'
),
)
);
return $meta_boxes;
}
add_filter('cmb_meta_boxes', 'base_meta_boxes_ba');
I'm using the WPAlchemy class to create a metabox. I want to place this metabox in a number of post editors in the backend.
Currently it's working just fine with the following code:
$video_metabox = new WPAlchemy_MetaBox(array
(
'id' => '_videoMeta',
'title' => 'Videos',
'types' => array('characters','homepage'),
'template' => THEMEASSETS . '/functions/video_meta.php'
));
What I want to do though is additionally place the metabox on the post editor for post ID #22. Supposedly the following code should work:
$video_metabox = new WPAlchemy_MetaBox(array
(
'id' => '_videoMeta',
'title' => 'Videos',
'types' => array('characters','homepage'),
'template' => THEMEASSETS . '/functions/video_meta.php',
'include_post_id' => 22
));
But it doesn't work unless I add in 'page' into the array of post types, which adds the metabox to all pages (not just post ID 22).
Is there a way to use poth the types and the include post ID arguments?
I had the same issue. Acutally, I had set a metabox to two custom post types and wanted it to be displayed on a specific page.
$video_metabox = new WPAlchemy_MetaBox(array(
'id' => '_videoMeta',
'title' => 'Videos',
'types' => array('characters','homepage', 'page'),
'template' => THEMEASSETS . '/functions/video_meta.php',
'include_post_id' => 22
));
Just add the bult in 'page' post type and everything should work fine. It did for me.