Forums Forums Search Search Results for 'save_post add_action'

Viewing 10 results - 1 through 10 (of 16 total)
  • Author
    Search Results
  • #257057

    Anonymous
    Inactive

    <?php
    if ( ! defined( ‘ABSPATH’ ) ) { exit; }

    if ( ! isset( $content_width ) ) $content_width = 1280;

    function skelementor_init() {

    add_theme_support( ‘title-tag’ );
    add_theme_support( ‘post-thumbnails’ );
    add_theme_support( ‘automatic-feed-links’ );
    add_theme_support( ‘title-tag’ );
    add_theme_support( ‘html5’, array( ‘search-form’, ‘comment-form’, ‘comment-list’, ‘gallery’, ‘caption’ ) );
    add_theme_support( ‘custom-logo’, array(
    ‘width’ => 260,
    ‘height’ => 100,
    ‘flex-height’ => true,
    ‘flex-width’ => true,
    ) );
    add_theme_support( ‘custom-header’ );
    add_theme_support( ‘woocommerce’ );
    add_post_type_support( ‘page’, ‘excerpt’ );

    register_nav_menus(
    array( ‘main-menu’ => __( ‘Main Menu’, ‘skelementor’ ) )
    );

    load_theme_textdomain( ‘skelementor’, get_template_directory() . ‘/languages’ );
    }
    add_action( ‘after_setup_theme’, ‘skelementor_init’ );

    function skelementor_comment_reply() {
    if ( get_option( ‘thread_comments’ ) ) { wp_enqueue_script( ‘comment-reply’ ); }
    }
    add_action( ‘comment_form_before’, ‘skelementor_comment_reply’ );

    function skelementor_scripts_styles() {
    wp_enqueue_style( ‘skelementor-style’, get_stylesheet_uri() );
    }
    add_action( ‘wp_enqueue_scripts’, ‘skelementor_scripts_styles’ );

    function skelementor_register_elementor_locations( $elementor_theme_manager ) {
    $elementor_theme_manager->register_all_core_location();
    };
    add_action( ‘elementor/theme/register_locations’, ‘skelementor_register_elementor_locations’ );

    function price_range_on_save( $post_id ) {
    $post_type = ‘share’; //custom post type for events

    //Check if we are saving correct post type
    if( get_post_type( $post_id ) != $post_type)
    return;

    //Check it’s not an auto save routine
    if( defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE )
    return;

    // get the price of the post. This assumes that the price is a number and has no currency symbol or thousands separator
    $post_price = get_post_field (‘price’, $post_id);
    // set an empty default string
    $post_price_text = “”;
    // set the price text label to match the price
    if ($post_price) {
    switch($post_price) {
    case ( $post_price <= 100 ):
    $post_price_text = “0-100 kr”;
    break;
    case ( ($post_price > 100) && ($post_price <= 250) ):
    $post_price_text = “101-250 kr”;
    break;
    default:
    $post_price_text = “250 kr and up”;
    }
    }

    // update the price choice
    update_field( ‘post_price_text’, $post_price_text );
    }
    add_action(‘save_post’, ‘price_range_on_save’);

    /////////
    $roots_includes = array(
    ‘lib/extras.php’,
    ‘lib/scripts.php’,
    ‘lib/ajax.php’,
    ‘lib/post-types.php’,
    ‘lib/init.php’,
    ‘lib/utilities.php’,
    ‘lib/acf.php’,
    // ‘lib/custom-fields.php’,
    // ‘lib/custom-fields/category.php’,
    // ‘lib/custom-fields/footer.php’,
    // ‘lib/custom-fields/post.php’,
    );

    foreach ($roots_includes as $file) {
    if (!$filepath = locate_template($file)) {
    trigger_error(sprintf(__(‘Error locating %s for inclusion’, ‘roots’), $file), E_USER_ERROR);
    }

    require_once $filepath;
    }
    unset($file, $filepath);

    // CUSTOM FILTER // Add author of the current post (SPELTIPS)
    add_action( ‘elementor/query/from_same-author’, function( $query ) {
    // The author of the current post
    $author_id = get_the_author_meta(‘ID’);
    $query->set( ‘author’, $author_id );
    } );

    // CUSTOM FILTER // Add Speltips from same expert on EXPERT page
    add_action( ‘elementor/query/spel-from-same-expert’, function( $query ) {
    // The author of the current post

    $author_id = get_field( ‘wp_authors’ );

    $query->set( ‘author’, $author_id );
    } );

    add_action( ‘elementor/query/shares-from-current_author’, function( $query ) {
    // The author of the current post

    $author_id = get_the_author_meta(‘ID’);

    $expert_id = get_field( ‘expert’, ‘user_’ . $author_id );

    if( is_array( $expert_id ) ) $expert_id = $expert_id[ ‘ID’ ];

    $meta_query = $query->get( ‘meta_query’ );

    if( ! is_array( $meta_query ) ) $meta_query = [];

    $meta_query[ ‘expert_id’ ] = [
    ‘key’ => ‘expert’,
    ‘compare’ => ‘==’,
    ‘value’ => $expert_id,
    ];

    $query->set( ‘meta_query’, $meta_query );
    } );

    // CUSTOM FILTER // Add ANDELAR (SHARES) from same expert on EXPERT page
    add_action( ‘elementor/query/andelar-from-same-expert’, function( $query ) {
    // The author of the current post
    $expert_id = get_the_ID();
    $meta_query = $query->get( ‘meta_query’ );

    if( ! is_array( $meta_query ) ) $meta_query = [];

    $meta_query[ ‘expert_id’ ] = [
    ‘key’ => ‘expert’,
    ‘compare’ => ‘==’,
    ‘value’ => $expert_id,
    ];

    $query->set( ‘meta_query’, $meta_query );
    } );

    // Add Options Page
    function add_my_options_page() {
    if( function_exists(‘acf_add_options_page’) ) {
    acf_add_options_page();
    }
    }
    add_action( ‘plugins_loaded’, ‘add_my_options_page’ );

    // Don’t put anything except includes in this file
    //Pick a place for your jsons to live. We stick them into the /theme/includes/acf-json folder, but you can stick these wherever

    // change where acf json files are saved
    add_filter(‘acf/settings/save_json’, ‘jb_acf_save_json’);
    function jb_acf_save_json($path) { return get_stylesheet_directory().’/includes/acf-json’; }

    // add our custom folder to places acf json are loaded from
    add_filter(‘acf/settings/load_json’, ‘jb_acf_load_json’);
    function jb_acf_load_json($paths) { return array_merge($paths, array(get_stylesheet_directory().’/includes/acf-json’)); }

    function custom_theme_slug_filter_the_content( $content ) {
    global $wpdb;
    if(“post” == get_post_type()){
    //if(is_singular(‘posts’)){
    $requested_post_id = get_the_ID();
    $authordetails = get_field(‘acfe_author’, $requested_post_id);
    $suthorname = $authordetails[‘display_name’];
    $expert_args = array(
    ‘post_type’ => ‘expert’,
    ‘post_status’ => ‘publish’,
    ‘title’ => trim($suthorname),
    ‘orderby’ => ‘date’,
    ‘order’ => ‘DESC’
    );
    $query = new WP_Query($expert_args);
    $postid = $query->post->ID;
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $postid ), ‘full’ );
    $custom_content = ‘<div id=”ctm-img-box” class=”custom-img-box”><span class=”elementor-icon-list-icon”></span><span class=”authorname”>’.$suthorname.'</span></div>’;
    $custom_content .= $content;
    //return $custom_content;
    }else{
    $custom_content = $content;
    }
    return $custom_content;
    }
    add_filter( ‘the_content’, ‘custom_theme_slug_filter_the_content’, 1);

    function wpa_choose_ad($user_id, $expert_id) {
    $requested_post_id = get_the_ID();
    $expert_user = get_field(‘wp_authors’, $requested_post_id);
    //echo $user_id;
    $args = array(
    ‘post_type’ => ‘share’,
    ‘posts_per_page’ => 12,
    ‘orderby’ => ‘rand’,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘expert’,
    ‘value’ => $expert_id,
    ‘compare’ => “=”
    )
    )
    );
    $the_query = new WP_Query( $args );
    //echo “

    ";
        //print_r($the_query);
        $the_ad = '<div class="elementor-container elementor-column-gap-no">';
        	$the_ad .= '<div class="element-row">';
        	$the_ad .= '<div class="elementor-widget-container elementor-widget-heading"><h4 class="elementor-heading-title elementor-size-default">MINA ANDELAR</h4></div>';
        $the_ad .= '<div class="elementor-widget-container">';	
        $the_ad .= '<div class="ecs-posts elementor-posts-container elementor-posts elementor-grid elementor-posts--skin-custom">';
        if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) : $the_query->the_post();
        		$title = get_the_title();
        		$url = get_permalink(get_the_ID());
    			$expert_type = get_field('type', get_the_ID());
    			$expert_shares = get_field('shares', get_the_ID());
    			$expert_price = get_field('price', get_the_ID());
    
    		$the_ad .= '<div class="share_outer_box">';
    		$the_ad .= '<div class="share_price_details">';
    		$the_ad .= '<p>'.$expert_type.'</p>';
    		$the_ad .= '<p>'.$expert_shares.' andler | '.$expert_price.' SEK</p>';
    		$the_ad .= '</div>';
    		$the_ad .= '<h2 class="custom-title elementor-heading-title elementor-size-default">'.$title.'</h2>';
    				   	
    		$the_ad .= '</div>';
        endwhile;
        }
        $the_ad .= '</div>';
        $the_ad .= '</div>';
        $the_ad .= '</div>';
        $the_ad .= '</div>';
        wp_reset_postdata();
    
        return $the_ad;
    }
    
    function filter_function_name( $query_args, $sfid ) {
    	
    	//if search form ID = 19856, the do something with this query
    	if($sfid==19856)
    	{
    		//modify $query_args here before returning it
    		// args
    			$args = array(
    				'post_type'		=> 'shares',
    				'meta_query'	=> array(
    				'relation'		=> 'OR',
    			array(
    			'key'		=> 'type',
    			'value'		=> 'Europatipset',
    			'compare'	=> 'LIKE'
    			),
    			array(
    			'key'		=> 'type',
    			'value'		=> 'Stryktipset',
    			'compare'	=> 'LIKE'
    			)
    	)
    );
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
    #256901

    Trevor
    Participant

    Code like this would have to be added to your child theme functions.php file:

    function price_range_on_save( $post_id ) {
      $post_type = 'share'; //custom post type for events
    
      //Check if we are saving correct post type
      if( get_post_type( $post_id ) != $post_type)
        return;
    
      //Check it's not an auto save routine
      if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return;
      
      // get the price of the post. This assumes that the price is a number and has no currency symbol or thousands separator
      $post_price = get_post_field('price', $post_id);
      // set an empty default string
      $post_price_text = "";
      // set the price text label to match the price
      if ($post_price) {
        switch($post_price) {
            case ( $post_price <= 100 ):
              $post_price_text = "0-100 kr";
              break;
            case ( ($post_price > 100) && ($post_price <= 250) ):
              $post_price_text = "101-250 kr";
              break;
            default:
              $post_price_text = "250 kr and up";
        }
      }
    
      // update the price choice
      update_field( 'post_price_text', $post_price_text );
    }
    add_action('save_post', 'price_range_on_save');

    Assuming what you sent me. and, the problem is always around the breakpoints. Fr example, is exactly 100 in the first or second term? I have assumed the first, otherwise you may need to change the operators a little, like this:

    function price_range_on_save( $post_id ) {
      $post_type = 'share'; //custom post type for events
    
      //Check if we are saving correct post type
      if( get_post_type( $post_id ) != $post_type)
        return;
    
      //Check it's not an auto save routine
      if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return;
      
      // get the price of the post. This assumes that the price is a number and has no currency symbol or thousands separator
      $post_price = get_post_field('price', $post_id);
      // set an empty default string
      $post_price_text = "";
      // set the price text label to match the price
      if ($post_price) {
        switch($post_price) {
            case ( $post_price < 100 ):
              $post_price_text = "0-100 kr";
              break;
            case ( ($post_price >= 100) && ($post_price < 250) ):
              $post_price_text = "101-250 kr";
              break;
            default:
              $post_price_text = "250 kr and up";
        }
      }
    
      // update the price choice
      update_field( 'post_price_text', $post_price_text );
    }
    add_action('save_post', 'price_range_on_save');

    NOTE: I have NOT tested this. To test, go to post and simply save it. Does it add the correct label?

    You would need to edit current posts and simply save them to add the value.

    #252619

    In reply to: Meta tag Array


    Anonymous
    Inactive

    Sorry, I was using the wrong Meta field, the one with an underscore is the wrong one.
    Now that I selected the correct ACF field it is working! thanks.

    Here is the code I have used for reference:

    // Event Manager hack to store  well formatted date on save:
    
      function event_date_year_on_save( $post_id ) {
        $post_type = 'event'; //custom post type for events
      
        //Check if we are saving correct post type
        if( get_post_type( $post_id ) != $post_type)
          return;
      
        //Check it's not an auto save routine
        if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
          return;
        
        // get the event date of the post. Note this is in YYYYMMDD format
        $event_date = get_post_field ('_event_start_date', $post_id); // getting the meta field from Event Manager
      
        // remove "-" from the formatted date
        update_field( 'event_formatted_date_start', str_replace('-', '', $event_date), $post_id );
      }
      add_action('save_post', 'event_date_year_on_save');

    In the filter settings, I used Meta – Numerical ascending.

    #251016

    Anonymous
    Inactive

    I have searched this forum and have tried several recommendations (https://support.searchandfilter.com/forums/topic/not-showing-all-results-2/) without any luck. Here’s my setup: WP All Import is importing data on an hourly basis. In the import settings, I have “Increase speed by disabling do_action calls in wp_insert_post during import” checked which means ‘transition_post_status’, ‘save_post’, ‘pre_post_update’, ‘add_attachment’, ‘edit_attachment’, ‘edit_post’, ‘post_updated’, ‘wp_insert_post’ are not being fired. I know that is why Search & Filter’s cache is not being updated. If I manually update cache, then search results are updated.

    As a solution, I’m trying to get wp-cron to update Search & Filter’s cache, but it’s not working either. Is there an error in my code?

    add_action( 'save_to_cache_schedule', 'save_to_cache' );
    function save_to_cache() {
        $args = array(
            'post_type' => 'property',
            'posts_per_page' => -1,
            'fields' => 'ids',
            'cache_results' => false,
            'no_found_rows' => true
        );
        $posts = new WP_Query( $args );
        foreach( (array)$posts->posts as $post_id ) {
            do_action('search_filter_update_post_cache', $post_id);
        }
    }
    add_action( 'init', 'register_save_to_cache_schedule');
    function register_save_to_cache_schedule() {
        if( !wp_next_scheduled( 'save_to_cache_schedule' ) ) {
            // Schedule the event
            wp_schedule_event( time(), 'twicehourly', 'save_to_cache_schedule' );
        }
    }
    add_filter( 'cron_schedules', 'cron_interval_twice_hourly' );
    function cron_interval_twice_hourly( $schedules ) {
        $schedules['twicehourly'] = array(
                'interval'  => 1800,
                'display'   => 'Twice Hourly'
        );
        return $schedules;
    }

    I have tried triggering the do_action(‘search_filter_update_post_cache’, $post_id); function in my theme’s functions.php file, in WP All Import’s function editor, and now in a scheduled function. Any help would be super appreciated!

    #248116

    Anonymous
    Inactive

    I see that you previously suggested that you solve this problem with save_post+add_action
    , whether it still works
    https://support.searchandfilter.com/forums/topic/acf-city-selector-field/

    #238869

    In reply to: Datefilter


    Trevor
    Participant

    So, in the child theme functions.php I have added this code at the end:

    function event_date_year_on_save( $post_id ) {
      $post_type = 'dig_veranstaltungen'; //custom post type for events
    
      //Check if we are saving correct post type
      if( get_post_type( $post_id ) != $post_type)
        return;
    
      //Check it's not an auto save routine
      if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return;
      
      // get the event date of the post. Note this is in YYYYMMDD format
      $event_date = get_post_field ('veranstaltungs_datum_beginn', $post_id);
    
      // update the event year value 
      update_field( 'jahr', substr( $event_date , 0, 4 ), $post_id );
    }
    add_action('save_post', 'event_date_year_on_save');

    It is really easy to get the year because it is the first four characters of the event date!

    Try it for yourself. Edit and event and change the event date (to a different year). No need to change the Year (jahr) you will see it changes automatically.

    I also added a little bit of CSS (in the theme customizer) to make the form nicer, and the year field also, plus German labels.

    Once you are happy that works, make these changes:

    https://www.screencast.com/t/yYKJTzoMp3

    Now the user will not see this field in the admin editor.

    All you have to do is edit the events and save. You do not actually need to make any changes, the save is to trigger the code to add the year.

    #238609

    In reply to: Datefilter


    Trevor
    Participant

    I found a post that will help us:

    https://support.searchandfilter.com/forums/topic/author-field-not-honouring-auto-count-setting/#post-223172

    The code used there was this:

    // save Author to author 
    function event_author_on_save( $post_id ) {
      $post_type = 'event'; //custom post type - change to post if you're just working with posts
    
      //Check if we are saving correct post type
      if( get_post_type( $post_id ) != $post_type)
        return;
    
      //Check it's not an auto save routine
      if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return;
      
      // get the author ID of the post
      $author_id = get_post_field ('post_author', $post_id);
      // get the display name of that author
      $author = get_the_author_meta( 'display_name' , $author_id );
    
      // update the custom field value 
      update_field( 'acf_event_author', $author, $post_id );
    }
    add_action('save_post', 'event_author_on_save');
    #223172

    Anonymous
    Inactive

    Thanks Trevor,
    Hopefully it’ll get implemented in the future.

    For anyone else facing this problem, I ended up creating a custom field as Trevor mentioned, setting it to hidden, then using a save action to add the author name to that field. (I needed this to be automatic, I didn’t want to rely on people having to tell the system who they are when adding posts)

    Here’s code that saves Author display name to a custom field if anyones interested:

    // save Author to author 
    function event_author_on_save( $post_id ) {
      $post_type = 'event'; //custom post type - change to post if you're just working with posts
    
      //Check if we are saving correct post type
      if( get_post_type( $post_id ) != $post_type)
        return;
    
      //Check it's not an auto save routine
      if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return;
      
      // get the author ID of the post
      $author_id = get_post_field ('post_author', $post_id);
      // get the display name of that author
      $author = get_the_author_meta( 'display_name' , $author_id );
    
      // update the custom field value 
      update_field( 'acf_event_author', $author, $post_id );
    }
    add_action('save_post', 'event_author_on_save');

    Anonymous
    Inactive

    Hi,

    I’m developing an events listing here:

    http://bollandlowe.net/dev/watford/visit/whats-on/

    It uses Events Manager, and Search and Filter Pro to filter the listing. The category filtering and search is working fine, but I’m having a problem with date ranges.

    I saw your other posts about the incompatibility between how Events Manager saves dates and how S&F works with them, and wrote this hook:

    
    // Convert Events Manager dates yyyy-mm-dd to yyyymmdd and save to an ACF field, for Search & Filter Pro to use
    
    function save_event_meta( $post_id ){
    
    if (get_post_type($post_id) == 'event'):
    
    $filter_event_start_date = date("Ymd", strtotime(get_post_meta($post_id, '_event_start_date', true)));
    $filter_event_end_date = date("Ymd", strtotime(get_post_meta($post_id, '_event_end_date', true)));
    
    update_field( 'filter_event_start_date', $filter_event_start_date , $post_id );
    update_field( 'filter_event_end_date', $filter_event_end_date , $post_id );
    
    endif;
    
    }
    add_action('acf/save_post', 'save_event_meta', 20); // run before ACF saves the $_POST['fields'] data
    

    This converts the start and end dates from yyyy-mm-dd to yyyymmdd and saves them to fields. Numerical sorting based on this start date field works.

    However, when I try to do a date range filter it doesn’t return any results. I’m at a bit of a loss, and was wondering if it’s because the querystring the datepicker creates is in a different format?

    Any suggestions would be appreciated.

    Thanks!

    #196329

    Trevor
    Participant

    Great to speak with you Chris. If you do this search:

    https://support.searchandfilter.com/forums/search/save_post+add_action/

    There are posts with code snippets that might help you take the three location bits of data from the array and save them into separate ACF fields, for use with our plugin.

Viewing 10 results - 1 through 10 (of 16 total)