Filtering custom posts on Elementor using the value of a custom field - wordpress

I am using the Reveal theme to create a listing website and I recently discovered the Advanced Custom Forms (Custom fields).
On my homepage, there is an Elementor block of this Reveal plugin that allows me to display custom posts.
I'd like to adapt an Elementor widget to filter out listings with a value of 1 in the "FreeListing" custom field (basically exclude them).
I saw that this link offered the solution : How to Filter Elementor Posts By a Custom Field's value in wordpress To make a long story short, everyone adds this to add_action, according to my research, but I have never seen this in my home.
But in my code I don't see how to introduce this. Do you know if I'm editing in the wrong place, or if I can do it here?
<?php
namespace Reveal\Core\Elementor\Widgets;
use Elementor\Controls_Manager;
use Elementor\Widget_Base;
class Popular_Listings_TwoNoFreeListing extends Widget_Base {
public function get_name() {
return 'reveal_popular_listings_2';
}
public function get_title() {
return esc_html__( 'Popular Listings No Free', 'reveal-core' );
}
public function get_icon() {
return 'eicon-banner';
}
public function get_categories() {
return array( 'reveal-core' );
}
protected function register_controls() {
$this->start_controls_section(
'general',
array(
'label' => esc_html__( 'General', 'reveal-core' ),
)
);
$this->add_control(
'title',
array(
'label' => esc_html__( 'Title', 'reveal-core' ),
'label_block' => true,
'type' => Controls_Manager::TEXT,
'default' => __( 'Most Popular Grid', 'reveal-core' ),
)
);
$this->add_control(
'showposts',
array(
'label' => esc_html__( 'Number Of Listing', 'reveal-core' ),
'type' => Controls_Manager::TEXT,
'default' => 6,
)
);
$this->add_control(
'orderby',
array(
'label' => esc_html__( 'Order By', 'reveal-core' ),
'type' => Controls_Manager::SELECT,
'options' => array(
'date' => esc_html__( 'Date', 'reveal-core' ),
'id' => esc_html__( 'ID', 'reveal-core' ),
'title' => esc_html__( 'Title', 'reveal-core' ),
'name' => esc_html__( 'Name', 'reveal-core' ),
'modified' => esc_html__( 'Modified', 'reveal-core' ),
'rand' => esc_html__( 'Random', 'reveal-core' ),
),
'default' => 'date',
)
);
$this->add_control(
'order',
array(
'label' => esc_html__( 'Sort Order', 'reveal-core' ),
'type' => Controls_Manager::SELECT,
'options' => array(
'desc' => esc_html__( 'Descending', 'reveal-core' ),
'asc' => esc_html__( 'Ascending', 'reveal-core' ),
),
'default' => 'desc',
)
);
$this->add_control(
'extra_class',
array(
'label' => esc_html__( 'Extra Class', 'reveal-core' ),
'label_block' => true,
'type' => Controls_Manager::TEXT,
'default' => 'popular-listing',
)
);
$this->end_controls_section();
$this->start_controls_section(
'style_section',
array(
'label' => __('Color Option', 'reveal-core'),
'tab' => \Elementor\Controls_Manager::TAB_STYLE,
)
);
$this->start_controls_tabs( 'tabs_button_style' );
$this->start_controls_tab(
'tab_button_normal',
array(
'label' => __( 'Normal', 'elementor' ),
)
);
$this->add_control(
'icon_color',
array(
'label' => __('Icon Color', 'reveal-core'),
'separator' => 'before',
'type' => \Elementor\Controls_Manager::COLOR,
'selectors' => array(
'{{WRAPPER}} .cat-icon i' => 'background: {{VALUE}} !important',
),
)
);
$this->add_control(
'icon_color_two',
array(
'label' => __('Icon Color Two', 'reveal-core'),
'separator' => 'before',
'type' => \Elementor\Controls_Manager::COLOR,
'selectors' => array(
'{{WRAPPER}} .bg-d' => 'background: {{VALUE}} !important',
),
)
);
$this->add_control(
'rating_color',
array(
'label' => __('Rating Color', 'reveal-core'),
'separator' => 'before',
'type' => \Elementor\Controls_Manager::COLOR,
'selectors' => array(
'{{WRAPPER}} .property_item .list-rate' => 'background: {{VALUE}} !important',
),
)
);
$this->add_control(
'checkek',
array(
'label' => __('Checked Icon Color', 'reveal-core'),
'separator' => 'before',
'type' => \Elementor\Controls_Manager::COLOR,
'selectors' => array(
'{{WRAPPER}} span.veryfied-author:before' => 'background: {{VALUE}} !important',
),
)
);
$this->add_control(
'status_color',
array(
'label' => __('Status Color', 'reveal-core'),
'separator' => 'before',
'type' => \Elementor\Controls_Manager::COLOR,
'selectors' => array(
'{{WRAPPER}} .place-status.closed' => 'color: {{VALUE}} !important',
),
)
);
$this->end_controls_tab();
$this->start_controls_tab(
'tab_button_hover',
array(
'label' => __( 'Hover', 'elementor' ),
)
);
$this->add_control(
'title_color',
array(
'label' => __('Title Hover Color', 'reveal-core'),
'separator' => 'before',
'type' => \Elementor\Controls_Manager::COLOR,
'selectors' => array(
'{{WRAPPER}} .proerty_text .captlize a:hover' => 'color: {{VALUE}} !important',
'{{WRAPPER}} .listing-cat a:hover' => 'color: {{VALUE}} !important',
),
)
);
$this->end_controls_tab();
$this->end_controls_tabs();
$this->end_controls_section();
}
protected function render() {
$settings = $this->get_settings_for_display();
$extra_class = $settings['extra_class'];
$title = $settings['title'];
$showposts = $settings['showposts'];
$orderby = $settings['orderby'];
$order = $settings['order'];
$args = array(
'post_type' => 'rlisting',
'post_status' => 'publish',
'showposts' => $showposts,
'orderby' => $orderby,
'order' => $order,
);
$listing_query = new \WP_Query( $args );
?>
<!-- ================ List Grid Style ======================= -->
<section class="<?php echo $extra_class; ?>">
<div class="container">
<div class="row">
<!-- Single List -->
<?php
while ($listing_query->have_posts()) {
$listing_query->the_post();
?>
<div class="col-lg-4 col-md-6 col-sm-12">
<?php
echo do_shortcode('[classical-list-item]');
?>
</div>
<?php
}
?>
</div>
</div>
</section>
<!-- ============================ Listings End ================================== -->
<?php
}
}
Thanks for the help in clarifying

Related

SSL breaks a users ability to upload an image

I have a problem that is driving me crazy. I'm working on a business listing website in WordPress which allows business users to upload their businesses, along with a description and an image.
All was working fine in development but as soon as a SSL is installed on the site, image uploads produce an error:
An error occurred in the upload. Please try again later.
The inspector console shows:
400 error: Failed to load resource: the server responded with a status of 400 (Bad Request)
and the file cannot be uploaded.
I'm out of my depth here - any ideas please?
I think this is the relevant part of the theme library - not sure it it helps?
<?php
if ( ! search_and_go_elated_listing_plugin_installed() ) {
//exit if listing plugin is not installed
return;
}
if(!function_exists('search_and_go_elated_map_listing_type_settings')) {
function search_and_go_elated_map_listing_type_settings() {
$meta_box_listing_type = search_and_go_elated_create_meta_box(array(
'scope' => 'listing-type-item',
'title' => 'Listing Type Settings',
'name' => 'listing_type_settings_meta_box'
));
search_and_go_elated_create_meta_box_field(array(
'name' => 'eltd_listing_type_show_phone',
'type' => 'yesno',
'label' => esc_html__( 'Show Phone Field', 'search-and-go' ),
'default_value' => 'yes',
'parent' => $meta_box_listing_type
));
search_and_go_elated_create_meta_box_field(array(
'name' => 'eltd_listing_type_show_website',
'type' => 'yesno',
'label' => esc_html__( 'Show Website Field', 'search-and-go' ),
'default_value' => 'yes',
'parent' => $meta_box_listing_type
));
search_and_go_elated_create_meta_box_field(array(
'name' => 'eltd_listing_type_show_email',
'type' => 'yesno',
'label' => esc_html__( 'Show Email Field', 'search-and-go' ),
'default_value' => 'yes',
'parent' => $meta_box_listing_type
));
search_and_go_elated_create_meta_box_field(array(
'name' => 'eltd_listing_type_show_gallery',
'type' => 'yesno',
'label' => esc_html__( 'Show Gallery Images', 'search-and-go' ),
'default_value' => 'yes',
'parent' => $meta_box_listing_type
));
search_and_go_elated_create_meta_box_field(array(
'name' => 'eltd_listing_type_show_video',
'type' => 'yesno',
'label' => esc_html__( 'Show Video', 'search-and-go' ),
'default_value' => 'yes',
'parent' => $meta_box_listing_type
));
search_and_go_elated_create_meta_box_field(array(
'name' => 'eltd_listing_type_show_audio',
'type' => 'yesno',
'label' => esc_html__( 'Show Audio', 'search-and-go' ),
'default_value' => 'yes',
'parent' => $meta_box_listing_type
));
search_and_go_elated_create_meta_box_field(array(
'name' => 'eltd_listing_type_show_work_hours',
'type' => 'yesno',
'label' => esc_html__( 'Show Working Hours', 'search-and-go' ),
'default_value' => 'yes',
'parent' => $meta_box_listing_type
));
search_and_go_elated_create_meta_box_field(array(
'name' => 'eltd_listing_type_show_social_icons',
'type' => 'yesno',
'label' => esc_html__( 'Show Social Icons', 'search-and-go' ),
'default_value' => 'yes',
'parent' => $meta_box_listing_type
));
search_and_go_elated_create_meta_box_field(array(
'name' => 'eltd_listing_type_show_price',
'type' => 'yesno',
'label' => esc_html__( 'Show Price', 'search-and-go' ),
'default_value' => 'yes',
'parent' => $meta_box_listing_type
));
search_and_go_elated_create_meta_box_field(array(
'name' => 'eltd_listing_type_show_sidebar_gallery',
'type' => 'yesno',
'label' => esc_html__( 'Show Sidebar Gallery', 'search-and-go' ),
'description' => '',
'default_value' => 'yes',
'parent' => $meta_box_listing_type
));
search_and_go_elated_create_meta_box_field(array(
'name' => 'eltd_listing_type_show_booking_form',
'type' => 'yesno',
'label' => esc_html__( 'Show Booking Form', 'search-and-go' ),
'description' => esc_html__( 'Requires Elated Booking Plugin to be installed', 'search-and-go' ),
'default_value' => 'yes',
'parent' => $meta_box_listing_type
));
//init icon pack hide and show array. It will be populated dinamically from collections array
$listing_type_icon_pack_hide_array = array();
$listing_type_icon_pack_show_array = array();
//do we have some collection added in collections array?
if (is_array(search_and_go_elated_icon_collections()->iconCollections) && count(search_and_go_elated_icon_collections()->iconCollections)) {
//get collections params array. It will contain values of 'param' property for each collection
$listing_type_icon_collections_params = search_and_go_elated_icon_collections()->getIconCollectionsParams();
//foreach collection generate hide and show array
foreach (search_and_go_elated_icon_collections()->iconCollections as $dep_collection_key => $dep_collection_object) {
$listing_type_icon_pack_hide_array[$dep_collection_key] = '';
//we need to include only current collection in show string as it is the only one that needs to show
$listing_type_icon_pack_show_array[$dep_collection_key] = '#eltd_listing_type_icon_' . $dep_collection_object->param . '_container';
//for all collections param generate hide string
foreach ($listing_type_icon_collections_params as $listing_icon_collections_param) {
//we don't need to include current one, because it needs to be shown, not hidden
if ($listing_icon_collections_param !== $dep_collection_object->param) {
$listing_type_icon_pack_hide_array[$dep_collection_key] .= '#eltd_listing_type_icon_' . $listing_icon_collections_param . '_container,';
}
}
//remove remaining ',' character
$listing_type_icon_pack_hide_array[$dep_collection_key] = rtrim($listing_type_icon_pack_hide_array[$dep_collection_key], ',');
}
}
search_and_go_elated_create_meta_box_field(
array(
'parent' => $meta_box_listing_type,
'type' => 'select',
'name' => 'listing_type_icon_pack',
'default_value' => 'font_awesome',
'label' => esc_html__( 'Listing Type Icon Pack', 'search-and-go' ),
'description' => esc_html__( 'Choose icon pack for listing', 'search-and-go' ),
'options' => search_and_go_elated_icon_collections()->getIconCollections(),
'args' => array(
'dependence' => true,
'hide' => $listing_type_icon_pack_hide_array,
'show' => $listing_type_icon_pack_show_array
)
)
);
if (is_array(search_and_go_elated_icon_collections()->iconCollections) && count(search_and_go_elated_icon_collections()->iconCollections)) {
//foreach icon collection we need to generate separate container that will have dependency set
//it will have one field inside with icons dropdown
foreach (search_and_go_elated_icon_collections()->iconCollections as $collection_key => $collection_object) {
$icons_array = $collection_object->getIconsArray();
//get icon collection keys (keys from collections array, e.g 'font_awesome', 'font_elegant' etc.)
$icon_collections_keys = search_and_go_elated_icon_collections()->getIconCollectionsKeys();
//unset current one, because it doesn't have to be included in dependency that hides icon container
unset($icon_collections_keys[array_search($collection_key, $icon_collections_keys)]);
$listing_icon_hide_values = $icon_collections_keys;
$listing_icon_container = search_and_go_elated_add_admin_container(
array(
'parent' => $meta_box_listing_type,
'name' => 'listing_type_icon_' . $collection_object->param . '_container',
'hidden_property' => 'listing_type_icon_pack',
'hidden_value' => '',
'hidden_values' => $listing_icon_hide_values
)
);
search_and_go_elated_create_meta_box_field(
array(
'parent' => $listing_icon_container,
'type' => 'select',
'name' => 'listing_type_icon_' . $collection_object->param,
'default_value' => '',
'label' => esc_html__( 'Listing Type Icon', 'search-and-go' ),
'description' => esc_html__( 'Choose Listing Type Icon', 'search-and-go' ),
'options' => $icons_array,
)
);
}
}
search_and_go_elated_add_custom_fields_creator(array(
'name' => 'listing_custom_fields' ,
'label' => esc_html__( 'Custom Fields Creator', 'search-and-go' ),
'desciption' => esc_html__( 'Create listing type custom fields', 'search-and-go' ),
'parent' => $meta_box_listing_type
));
$feature_list_title = search_and_go_elated_add_admin_section_title(
array(
'parent' => $meta_box_listing_type,
'title' => esc_html__( 'Listing Type Feature List', 'search-and-go' ),
'name' => 'listing_type_feature_list_title'
)
);
search_and_go_elated_add_repeater_field(array(
'name' => 'eltd_listing_type_repeater',
'parent' => $meta_box_listing_type,
'fields' => array(
array(
'type' => 'textsimple',
'name' => 'eltd_listing_type_feature_list',
'label' => '',
'description' => '',
),
)
)
);
}
add_action('search_and_go_elated_meta_boxes_map', 'search_and_go_elated_map_listing_type_settings');
}

Random Products are not showing the amount of post which i include in posts_per_page

Random Products are not showing the amount of post which i include in posts_per_page
this is the criteria which i used to call products
$cpsc_args = array(
'post_type' => 'product',
'posts_per_page' => 8,
'order' => 'desc',
'orderby' => 'rand',
'tax_query' => array(
array(
'taxonomy' => 'pwb-brand',
'field' => 'term_id',
'terms' => $cpsc_term_id_display,
'operator' => 'IN'
)
)
);
$cpsc_query = new WP_Query( $cpsc_args );
now the results amount of products are shuffling sometimes it shows 7 products or 8 sometimes or 9 on every refresh.
here is the plugin file code where i loop the plugin.
<?php
/*
Plugin Name: Sultan Custom Brands
Description: This will Display All the Posts or any Post type as you want.
Version: 1.0
Author: Sultan Gillani
Author URI: https://nya.smyckendahls.se/
Text Domain: sultan-cpsc-plugin
*/
//include_once('functions.php');
function form_create($attr){
$cpsc_terms = get_terms( array(
'taxonomy' => 'pwb-brand',
'hide_empty' => false,
) );
$attr = shortcode_atts(array(
"cpsc_posts_per_page" => -1,
"cpsc_order" => "DESC",
"cpsc_orderby" => "date",
"cpsc_terms_show" => $cpsc_terms[0]->slug,
"cpsc_columns" => 4
),$attr);
$cpsc_term_id = get_term_by( 'slug', $attr['cpsc_terms_show'], 'pwb-brand');
$cpsc_term_id_display = $cpsc_term_id->term_id;
$cpsc_args = array(
'post_type' => 'product',
'posts_per_page' => $attr['cpsc_posts_per_page'],
'order' => $attr['cpsc_order'],
'orderby' => $attr['cpsc_orderby'],
'tax_query' => array(
array(
'taxonomy' => 'pwb-brand',
'field' => 'term_id',
'terms' => $cpsc_term_id_display,
'operator' => 'IN'
)
)
);
$cpsc_query = new WP_Query( $cpsc_args );
?>
<div class="woocommerce columns-<?php echo $attr['cpsc_columns']; ?>">
<?php
wc_get_template_part('loop/loop','start');
if ( $cpsc_query->have_posts() ) {
while ( $cpsc_query->have_posts() ) {
$cpsc_query->the_post();
wc_get_template_part('content','product');
}
// Restore original Post Data
wp_reset_postdata();
}
wc_get_template_part('loop/loop','end');
?>
</div>
<?php
}
add_shortcode('cpsc_code','form_create');
function cpsc_vc_element(){
$brand_arr = [];
$cpsc_terms_data = get_terms( array(
'taxonomy' => 'pwb-brand',
'hide_empty' => false,
) );
$brand_arr['Select The Brand'] = '';
foreach($cpsc_terms_data as $cpsc_term_data){
$brand_arr[$cpsc_term_data->name] =$cpsc_term_data->slug;
}
vc_map(
array(
'name' => __('Brand Query Builder'),
'base' => 'cpsc_code',
'category' => __('Test Shortcodes'),
"icon" => "icon-wpb-woocommerce", // Simply pass url to your icon here
"content_element" => true,
'admin_enqueue_css' => array('/wp-content/plugins/brand_plugin/cpsc.css'),
'params' => array(
array(
"type" => "number",
"holder" => "div",
"class" => "cpsc_plugin_posts_per_page",
"heading" => __( "Posts Per Page", "sultan-cpsc-plugin" ),
"param_name" => "cpsc_posts_per_page",
"value" => "0",
"description" => __( "Enter the number of post to display.", "sultan-cpsc-plugin" )
),
array(
"type" => "dropdown",
"holder" => "div",
"class" => "cpsc_plugin_order",
"heading" => __( "Order", "sultan-cpsc-plugin" ),
"param_name" => "cpsc_order",
'value' => array(
__( 'Orber By', "sultan-cpsc-plugin" ) => '',
__( 'Ascending', "sultan-cpsc-plugin" ) => 'ASC',
__( 'Descending', "sultan-cpsc-plugin" ) => 'DESC',
),
"description" => __( "Order Your Post", "sultan-cpsc-plugin" )
),
array(
"type" => "dropdown",
"holder" => "div",
"class" => "cpsc_plugin_orderby",
"heading" => __( "Order By", "sultan-cpsc-plugin" ),
"param_name" => "cpsc_orderby",
'value' => array(
__( 'Select The Orber With', "sultan-cpsc-plugin" ) => '',
__( 'Date', "sultan-cpsc-plugin" ) => 'date',
__( 'ID', "sultan-cpsc-plugin" ) => 'ID',
__( 'About the Author', "sultan-cpsc-plugin" ) => 'author',
__( 'Title', "sultan-cpsc-plugin" ) => 'title',
__( 'Modified', "sultan-cpsc-plugin" ) => 'modified',
__( 'Random', "sultan-cpsc-plugin" ) => 'rand',
__( 'Comment Count', "sultan-cpsc-plugin" ) => 'comment_count',
__( 'Menu Order', "sultan-cpsc-plugin" ) => 'menu_order',
__( 'Menu Order & title', "sultan-cpsc-plugin" ) => 'menu_order title',
),
"description" => __( "Order by the criteria.", "sultan-cpsc-plugin" )
),
array(
"type" => "dropdown",
"holder" => "div",
"class" => "cpsc_plugin_terms_show",
"heading" => __( "Brands", "sultan-cpsc-plugin" ),
"param_name" => "cpsc_terms_show",
'value' => $brand_arr,
"description" => __( "All Brands", "sultan-cpsc-plugin" )
),
array(
"type" => "textfield",
"holder" => "div",
"class" => "cpsc_plugin_columns",
"heading" => __( "Columns", "sultan-cpsc-plugin" ),
"param_name" => "cpsc_columns",
"value" => "4",
"description" => __( "", "sultan-cpsc-plugin" )
),
)
)
);
}
add_action('vc_before_init','cpsc_vc_element');
?>
Var_dump your:
$cpsc_terms[0]->slug
My quess is that the slug varies per page request? If so, the query will be based on a different term_id each request (if random). And term_id 1 might have 7 posts related, where term_id 2 has f.e. 9 posts related, etc. Hence the number of posts shown.

custom permalink structure for custom post type based on meta fields and custom taxonomy

I have two custom post types. One with the slug artwork and another with the slug research-materials. The permalink structure for an artwork post is http://example.com/artwork/artwork-slug and this is expected, works fine and what I want. On the post.php page for any post that is of the research-materials post type I have a custom field (via ACF) that has the slug of the artwork post that the research materials post is related to and the desired slug for the research materials post. There's also a custom taxonomy set for the research-materials post type which would play a part in the construction of the permalink structure for the research-materials posts. The permalink would look like http://example.com/artwork/related-artwork-slug/research-materials/custom-taxonomy-term/research-materials-slug
Here's the code I have so far but I'm not getting the result I'd expect... instead I'm redirected to the artwork custom post type when I should be redirected tot he research-materials custom post type single page.
Any help appreciated. Thanks in advance.
class RRP{
public static function init(){
// set up custom post types and custom taxonomies
add_action( 'init', 'RRP::build_custom_post_types' );
add_action( 'init', 'RRP::build_custom_taxonomy' );
// set up custom fields
add_action( 'acf/init', 'RRP::register_custom_fields' );
// validate saved value in custom fields
add_filter( 'acf/validate_value', 'RRP::validate_saved_value_in_custom_fields', 10, 4 );
// update related artwork slug
add_filter( 'acf/update_value', 'RRP::update_related_artwork_slug', 10, 3 );
// add the research material type
add_action( 'save_post_research-materials', 'RRP::set_research_material_type' );
// build rewrite rules
add_action( 'init', 'RRP::rewrite_stuff', 10, 0 );
add_filter( 'query_vars', 'RRP::build_query_vars', 10 );
add_filter( 'pre_get_posts', 'RRP::pre_get_posts', 10 );
}
public static function build_custom_post_types(){
$labels = array(
'name' => 'Research Materials',
'singular_name' => 'Research Material',
'add_new' => 'Add New Research Material',
'add_new_item' => 'Add New Research Material',
'edit_item' => 'Edit Research Material',
'new_item' => 'New Research Material',
'view_item' => 'View Research Material',
'search_items' => 'Search Research Materials',
'not_found' => 'No Research Materials found',
'not_found_in_trash' => 'No Research Materials found in Trash',
'parent_item_colon' => 'Parent Research Material:',
'menu_name' => 'Research Materials',
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'description',
'taxonomies' => array('research-material-type'),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => null,
'menu_icon' => null,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post',
'supports' => array(
'title', 'editor'
)
);
register_post_type( 'research-materials', $args );
}
public static function build_custom_taxonomy(){
$labels = array(
'name' => 'Research Material Types',
'singular_name' => 'Research Material Type',
'search_items' => 'Search Research Material Types',
'popular_items' => 'Popular Research Material Types',
'all_items' => 'All Research Material Types',
'parent_item' => 'Parent Research Material Type',
'parent_item_colon' => 'Parent Research Material Type',
'edit_item' => 'Edit Research Material Type',
'update_item' => 'Update Research Material Type',
'add_new_item' => 'Add New Research Material Type',
'new_item_name' => 'New Research Material Type Name',
'add_or_remove_items' => 'Add or remove Research Material Types',
'choose_from_most_used' => 'Choose from most used sfmomatheme',
'menu_name' => 'Research Material Type',
);
$args = array(
'labels' => $labels,
'public' => true,
'show_in_nav_menus' => true,
'show_admin_column' => false,
'hierarchical' => false,
'show_tagcloud' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'query_var' => true,
'capabilities' => array(),
);
register_taxonomy( 'research-material-type', array( 'research-materials' ), $args );
$terms = get_terms('research-material-type');
if( empty($terms) ){
wp_insert_term( 'Document', 'research-material-type', array(
'slug' => 'document',
) );
}
}
public static function register_custom_fields(){
acf_add_local_field_group(array(
'key' => 'group_research_materials',
'title' => 'Research Material Custom Fields',
'fields' => array(
array(
'key' => 'field_research_materials_slug',
'type' => 'text',
'name' => 'research_materials_slug',
'label' => 'Research Material Slug',
'required' => 1,
),
array(
'key' => 'field_research_materials_related_artwork',
'type' => 'relationship',
'name' => 'research_materials_related_artwork',
'label' => 'Related Artwork',
'post_type' => array(
'artwork',
),
'max' => 1,
'required' => 1,
),
array(
'key' => 'field_research_materials_related_artwork_slug',
'type' => 'text',
'name' => 'research_materials_related_artwork_slug',
'label' => 'Related Artwork Slug',
'disabled' => 1,
'instructions' => 'Generated based on Related Artwork',
)
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'research-materials',
),
)
),
));
}
public static function validate_saved_value_in_custom_fields($valid, $value, $field, $input){
if( empty($value) && $field['name'] !== 'research_materials_related_artwork_slug' ){
$valid = 'Field must not be empty!';
}
return $valid;
}
public static function set_research_material_type($post_id){
// build main term by default term or by whatever's the first result set
$terms = wp_get_object_terms( strval($post_id), 'research-material-type' );
$default_term = get_term_by( 'slug', 'document', 'research-material-type' );
if( empty($terms) ){
wp_set_object_terms( strval($post_id), $default_term->term_id, 'research-material-type' );
}
else{
wp_set_object_terms( strval($post_id), $terms[0]->term_id, 'research-material-type' );
}
}
public static function build_query_vars($vars){
$vars[] = 'research_materials_related_artwork_slug';
$vars[] = 'research_materials_slug';
$vars[] = 'research_materials_type';
return $vars;
}
public static function update_related_artwork_slug($value, $post_id, $field){
if( $field['name'] === 'research_materials_related_artwork_slug' ){
$value = get_field('research_materials_related_artwork', $post_id)[0]->post_name;
}
return $value;
}
public static function pre_get_posts($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;
}
$research_materials_related_artwork = get_query_var('research_materials_related_artwork_slug');
$research_materials_slug = get_query_var('research_materials_slug');
$research_materials_type = get_query_var('research_materials_type');
if( !empty($research_materials_related_artwork) && !empty($research_material_slug) && !empty($research_materials_type) ){
$meta_query = $query->get('meta_query');
if( empty($meta_query) ){
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'research_materials_related_artwork_slug',
'value' => $research_materials_related_artwork,
'compare' => '==',
),
array(
'key' => 'research_materials_slug',
'value' => $research_materials_slug,
'compare' => '==',
)
);
}
else{
$meta_query[] = array(
'relation' => 'AND',
array(
'key' => 'research_materials_related_artwork_slug',
'value' => $research_materials_related_artwork,
'compare' => '==',
),
array(
'key' => 'research_materials_slug',
'value' => $research_materials_slug,
'compare' => '==',
)
);
}
$query->set('meta_query', $meta_query);
$tax_query = $query->get('tax_query');
if( empty($tax_query) ){
$tax_query = array(
'relation' => 'AND',
array(
'taxonomy' => 'research-material-type',
'field' => 'slug',
'terms' => array($research_materials_type),
)
);
}
else{
$tax_query[] = array(
'relation' => 'AND',
array(
'taxonomy' => 'research-material-type',
'field' => 'slug',
'terms' => array($research_materials_type),
)
);
}
$query->set('tax_query', $tax_query);
}
return $query;
}
public static function rewrite_stuff(){
add_rewrite_tag( '%research_materials_related_artwork_slug%', '([^&]+)' );
add_rewrite_tag( '%research_materials_slug%', '([^&]+)' );
add_rewrite_rule( '^artwork/([^/]*)/research-materials/([^/]*)/([^/]*)/?', 'index.php?post_type=research-materials&research_materials_related_artwork_slug=$matches[1]&research_materials_slug=$matches[2]&research_materials_type=$matches[3]', 'top' );
}
}
RRP::init();
After much tinkering I figured it out. There were a lot of small things wrong with the above code and I also had to make use of the index_template hook. Here's the final code:
class RRP{
public static function init(){
// set up custom post types and custom taxonomies
add_action( 'init', 'RRP::build_custom_post_types' );
add_action( 'init', 'RRP::build_custom_taxonomy' );
// set up custom fields
add_action( 'acf/init', 'RRP::register_custom_fields' );
// validate saved value in custom fields
add_filter( 'acf/validate_value', 'RRP::validate_saved_value_in_custom_fields', 10, 4 );
// update related artwork slug
add_filter( 'acf/update_value', 'RRP::update_related_artwork_slug', 10, 3 );
// add the research material type
add_action( 'save_post_research-materials', 'RRP::set_research_material_type' );
// build rewrite rules
add_filter( 'index_template', 'RRP::point_artwork_and_artist_to_single_templates' );
add_action( 'init', 'RRP::rewrite_stuff', 0 );
add_filter( 'query_vars', 'RRP::build_query_vars', 10 );
add_filter( 'pre_get_posts', 'RRP::pre_get_posts', 10 );
}
public static function build_custom_post_types(){
$labels = array(
'name' => 'Research Materials',
'singular_name' => 'Research Material',
'add_new' => 'Add New Research Material',
'add_new_item' => 'Add New Research Material',
'edit_item' => 'Edit Research Material',
'new_item' => 'New Research Material',
'view_item' => 'View Research Material',
'search_items' => 'Search Research Materials',
'not_found' => 'No Research Materials found',
'not_found_in_trash' => 'No Research Materials found in Trash',
'parent_item_colon' => 'Parent Research Material:',
'menu_name' => 'Research Materials',
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'description',
'taxonomies' => array('research-material-type'),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => null,
'menu_icon' => null,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => array(
'slug' => 'research-materials',
'with_front' => false,
),
'capability_type' => 'post',
'supports' => array(
'title', 'editor'
)
);
register_post_type( 'research-materials', $args );
}
public static function build_custom_taxonomy(){
$labels = array(
'name' => 'Research Material Types',
'singular_name' => 'Research Material Type',
'search_items' => 'Search Research Material Types',
'popular_items' => 'Popular Research Material Types',
'all_items' => 'All Research Material Types',
'parent_item' => 'Parent Research Material Type',
'parent_item_colon' => 'Parent Research Material Type',
'edit_item' => 'Edit Research Material Type',
'update_item' => 'Update Research Material Type',
'add_new_item' => 'Add New Research Material Type',
'new_item_name' => 'New Research Material Type Name',
'add_or_remove_items' => 'Add or remove Research Material Types',
'choose_from_most_used' => 'Choose from most used sfmomatheme',
'menu_name' => 'Research Material Type',
);
$args = array(
'labels' => $labels,
'public' => true,
'show_in_nav_menus' => true,
'show_admin_column' => false,
'hierarchical' => false,
'show_tagcloud' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'query_var' => true,
'capabilities' => array(),
);
register_taxonomy( 'research-material-type', array( 'research-materials' ), $args );
$terms = get_terms('research-material-type');
if( empty($terms) ){
wp_insert_term( 'Document', 'research-material-type', array(
'slug' => 'document',
) );
}
}
public static function register_custom_fields(){
acf_add_local_field_group(array(
'key' => 'group_research_materials',
'title' => 'Research Material Custom Fields',
'fields' => array(
array(
'key' => 'field_research_materials_slug',
'type' => 'text',
'name' => 'research_materials_slug',
'label' => 'Research Material Slug',
'required' => 1,
),
array(
'key' => 'field_research_materials_related_artwork',
'type' => 'relationship',
'name' => 'research_materials_related_artwork',
'label' => 'Related Artwork',
'post_type' => array(
'artwork',
),
'max' => 1,
'required' => 1,
),
array(
'key' => 'field_research_materials_related_artwork_slug',
'type' => 'text',
'name' => 'research_materials_related_artwork_slug',
'label' => 'Related Artwork Slug',
'disabled' => 1,
'instructions' => 'Generated based on Related Artwork',
)
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'research-materials',
),
)
),
));
}
public static function validate_saved_value_in_custom_fields($valid, $value, $field, $input){
if( empty($value) && $field['name'] !== 'research_materials_related_artwork_slug' ){
$valid = 'Field must not be empty!';
}
return $valid;
}
public static function set_research_material_type($post_id){
// build main term by default term or by whatever's the first result set
$terms = wp_get_object_terms( strval($post_id), 'research-material-type' );
$default_term = get_term_by( 'slug', 'document', 'research-material-type' );
if( empty($terms) ){
wp_set_object_terms( strval($post_id), $default_term->term_id, 'research-material-type' );
}
else{
wp_set_object_terms( strval($post_id), $terms[0]->term_id, 'research-material-type' );
}
}
public static function build_query_vars($vars){
$vars[] = 'research_materials_related_artwork_slug';
$vars[] = 'research_materials_slug';
$vars[] = 'research_materials_type';
return $vars;
}
public static function update_related_artwork_slug($value, $post_id, $field){
if( $field['name'] === 'research_materials_related_artwork_slug' ){
$value = get_field('research_materials_related_artwork', $post_id)[0]->post_name;
}
return $value;
}
public static function pre_get_posts($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;
}
$research_materials_related_artwork = $query->get('research_materials_related_artwork_slug');
error_log(print_r($research_materials_related_artwork, true));
$research_materials_slug = $query->get('research_materials_slug');
error_log(print_r($research_materials_slug, true));
$research_materials_type = $query->get('research_materials_type');
error_log(print_r($research_materials_type, true));
if( !empty($research_materials_related_artwork) && !empty($research_materials_slug) && !empty($research_materials_type) ){
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'research_materials_related_artwork_slug',
'value' => $research_materials_related_artwork,
'compare' => '=',
),
array(
'key' => 'research_materials_slug',
'value' => $research_materials_slug,
'compare' => '=',
)
);
$query->set('meta_query', $meta_query);
$tax_query = array(
'relation' => 'AND',
array(
'taxonomy' => 'research-material-type',
'field' => 'slug',
'terms' => array($research_materials_type),
)
);
$query->set('tax_query', $tax_query);
}
return $query;
}
// needed because I also have a CPT called artwork
public static function point_artwork_and_artist_to_single_templates($templates = ''){
global $post;
if( $post->post_type == 'research-materials' ){
$templates = locate_template( 'single-research-materials.php' );
}
return $templates;
}
public static function rewrite_stuff(){
add_rewrite_tag( '%research_materials_related_artwork_slug%', '([^&]+)' );
add_rewrite_tag( '%research_materials_slug%', '([^&]+)' );
add_rewrite_tag( '%research_materials_type%', '([^&]+)' );
add_rewrite_rule( 'artwork\/(.*)\/research-materials\/(.*)\/(.*)\/?$', 'index.php?post_type=research-materials&research_materials_related_artwork_slug=$matches[1]&research_materials_type=$matches[2]&research_materials_slug=$matches[3]', 'top' );
}
}
RRP::init();

Update Parent post type in child theme

I'm developing a child theme of a premium template, this comes with a custom post type with the label name of "release" but I'd like to change it to something else, I know that if I go to the functions.php file of the main theme I can change it easily but I'd like to change it from my child theme?
here is the code
<?php
add_action('init', 'kentha_release_register_type');
if(!function_exists('kentha_release_register_type')){
function kentha_release_register_type() {
$labelsrelease = array(
'name' => esc_html__("Release",'kentha'),
'singular_name' => esc_html__("Release",'kentha'),
'add_new' => esc_html__("Add new",'kentha'),
'add_new_item' => esc_html__("Add new release",'kentha'),
'edit_item' => esc_html__("Edit release",'kentha'),
'new_item' => esc_html__("New release",'kentha'),
'all_items' => esc_html__("All releases",'kentha'),
'view_item' => esc_html__("View release",'kentha'),
'search_items' => esc_html__("Search release",'kentha'),
'not_found' => esc_html__("No releases found",'kentha'),
'not_found_in_trash' => esc_html__("No releases found in trash",'kentha'),
'menu_name' => esc_html__("Album releases",'kentha')
);
$args = array(
'labels' => $labelsrelease,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'page',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 40,
'page-attributes' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-controls-play',
'supports' => array('title', 'thumbnail','editor', 'page-attributes' )
);
if (function_exists('ttg_custom_post_type')){
ttg_custom_post_type( "release" , $args );
}
/* ============= create custom taxonomy for the releases ==========================*/
$labels = array(
'name' => __( 'Release genres','kentha' ),
'singular_name' => esc_html__( 'Genre','kentha' ),
'search_items' => esc_html__( 'Search by genre','kentha' ),
'popular_items' => esc_html__( 'Popular genres','kentha' ),
'all_items' => esc_html__( 'All releases','kentha' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => esc_html__( 'Edit genre','kentha' ),
'update_item' => esc_html__( 'Update genre','kentha' ),
'add_new_item' => esc_html__( 'Add New genre','kentha' ),
'new_item_name' => esc_html__( 'New genre Name','kentha' ),
'separate_items_with_commas' => esc_html__( 'Separate genres with commas','kentha' ),
'add_or_remove_items' => esc_html__( 'Add or remove genres','kentha' ),
'choose_from_most_used' => esc_html__( 'Choose from the most used genres','kentha' ),
'menu_name' => esc_html__( 'Music genres','kentha' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'genre' ),
);
if(function_exists('ttg_custom_taxonomy')){
ttg_custom_taxonomy('genre','release',$args );
}
$fields = array(
array( // Repeatable & Sortable Text inputs
'label' => esc_html__( 'Release Tracks','kentha' ), // <label>
'desc' => esc_html__( 'Add one for each track in the release','kentha' ), // description
'id' => 'track_repeatable', // field id and name
'type' => 'repeatable', // type of field
'sanitizer' => array( // array of sanitizers with matching kets to next array
'featured' => 'meta_box_santitize_boolean',
'title' => 'sanitize_text_field',
'desc' => 'wp_kses_data'
),
'repeatable_fields' => array ( // array of fields to be repeated
'releasetrack_mp3_demo' => array(
'label' => esc_html__( 'MP3 Demo','kentha' ),
'desc' => esc_html__( '(Never upload your full quality tracks, someone can steal them)','kentha' ), // description
'id' => 'releasetrack_mp3_demo',
'type' => 'file'
),
'releasetrack_track_title' => array(
'label' => esc_html__( 'Title','kentha' ),
'id' => 'releasetrack_track_title',
'type' => 'text'
),
'releasetrack_artist_name' => array(
'label' => esc_html__( 'Artists','kentha' ),
'desc' => esc_html__( '(All artists separated bu comma)','kentha' ), // description
'id' => 'releasetrack_artist_name',
'type' => 'text'
),
'releasetrack_buy_url' => array(
'label' => esc_html__( 'Track Buy link','kentha' ),
'desc' => esc_html__( 'A link to buy the single track','kentha' ), // description
'id' => 'releasetrack_buyurl',
'type' => 'text'
),
'icon_type' => array(
'label' => esc_html__( 'Track icon (cart icon is default)','kentha' ),
'id' => 'icon_type',
'type' => 'select',
'default' => 'cart',
'options' => array(
array('label' => 'cart','value' => 'cart'),
array('label' => 'download','value' => 'download')
)
),
)
)
);
$fields_links = array(
array( // Repeatable & Sortable Text inputs
'label' => esc_html__( 'Custom Buy Links','kentha' ), // <label>
'desc' => esc_html__( 'Add one for each link to external websites','kentha' ), // description
'id' => 'track_repeatablebuylinks', // field id and name
'type' => 'repeatable', // type of field
'sanitizer' => array( // array of sanitizers with matching kets to next array
'featured' => 'meta_box_santitize_boolean',
'title' => 'sanitize_text_field',
'desc' => 'wp_kses_data'
),
'repeatable_fields' => array ( // array of fields to be repeated
'custom_buylink_anchor' => array(
'label' => esc_html__( 'Custom Buy Text','kentha' ),
'desc' => esc_html__( '(example: Itunes, Beatport, Trackitdown)','kentha' ),
'id' => 'cbuylink_anchor',
'type' => 'text'
),
'custom_buylink_url' => array(
'label' => esc_html__('Custom Buy URL','kentha' ),
'desc' => esc_html__( '(example: http://...)','kentha' ), // description
'id' => 'cbuylink_url',
'type' => 'text'
)
)
)
);
$fields_release = array(
array(
'label' => esc_html__('Main artist', "kentha"),
'id' => 'releasetrack_artist',
'type' => 'post_chosen',
'posttype' => 'artist'
),
array(
'label' => esc_html__( 'Label','kentha' ),
'id' => 'general_release_details_label',
'type' => 'text'
),
array(
'label' =>esc_html__( 'Release date (YYYY-MM-DD)','kentha' ) ,
'id' => 'general_release_details_release_date',
'type' => 'date'
),
array(
'label' => esc_html__( 'Catalog Number','kentha' ),
'id' => 'general_release_details_catalognumber',
'type' => 'text'
)
);
$details_box = new custom_add_meta_box( 'release_details', 'Release Details', $fields_release, 'release', true );
$sample_box = new custom_add_meta_box( 'release_tracks', 'Release Tracks', $fields, 'release', true );
$buylinks_box = new custom_add_meta_box( 'release_buylinkss', 'Custom Buy Links', $fields_links, 'release', true );
}}
I need to update in Child Teme the icon_type options adding this extra fields
array('label' => 'spotify','value' => 'spotify')
Any suggestion?
Thanks in advance

WordPress customizer image output in front end, not working

I saw "Background image not saving in WordPress Customizer"
But didn't get any help.
My code is
function sorcey_customize_register($wp_customize){
$wp_customize->add_section('sorcey_footer', array(
'title' => __('Footer', 'sorcey'),
'description' => '',
'priority' => 120,
));
/* =============================
Footer logo
===============================*/
$wp_customize->add_setting('sorcey_footer_logo', array(
'default' => '',
'capability' => 'edit_theme_options',
'type' => 'text',
));
$wp_customize->add_control( new WP_Customize_Cropped_Image_Control(
$wp_customize, 'sorcey_footer_logo_ctrl', array(
'label' => __( 'Footer Logo', 'sorcey' ),
'section' => 'sorcey_footer',
'settings' => 'sorcey_footer_logo',
'height' => '40px',
'width' => '160px',
'button_labels' => array(
'select' => __( 'Select logo' ),
'change' => __( 'Change logo' ),
'remove' => __( 'Remove' ),
'default' => __( 'Default' ),
'placeholder' => __( 'No logo selected' ),
'frame_title' => __( 'Select logo' ),
'frame_button' => __( 'Choose logo' ),
),
)
));
}
I Tryed for output by
echo get_theme_mod( 'sorcey_footer_logo' );
And
echo wp_get_attachment_url( get_theme_mod( 'sorcey_footer_logo' ) );
And
var_dump(get_theme_mod( 'sorcey_footer_logo' ));
But showing 'boolean false'

Resources