• Resolved Vaughan

    (@vaughan-van-dyk)


    Hi,

    Great plugin, thanks! There’s not an easy way to search these help forums, so I’m not sure whether this has already been covered.

    I have a Description field in my metabox that I wanted to automatically use as the excerpt of the post (as this is what is by default used by Google search results etc). After some trial and error, I came up with the following code, which I’m including here to help anyone else looking for this, and to find out from the others here if there isn’t a simpler way of doing this.

    Thanks for your help.

    /* automatically assign the Description metabox field to the post excerpt */
    function metabox_to_excerpt($post_id){
    /* avoid infinite loop */
    	if ( ! wp_is_post_revision( $post_id ) ){
    
    		// unhook this function so it doesn't loop infinitely
    		remove_action('save_post', 'metabox_to_excerpt');	
    
    		$custom = rwmb_meta( 'description', '', $post_id );
    		wp_update_post(array('ID' => $post_id, 'post_excerpt' => $custom));
    
    		// re-hook this function
    		add_action('save_post', 'metabox_to_excerpt');
    	}
    }
    add_action('save_post', 'metabox_to_excerpt');

    https://wordpress.org/plugins/meta-box/

Viewing 2 replies - 1 through 2 (of 2 total)
  • That’s a good way of doing it, although it might be that you want the regular exceprt *and* your metabox version. In which case, you would do sth like:

    function filter_function_name( $excerpt ) {
      global $post;
      $custom = rwmb_meta( 'description', '', $post->ID);
      return $custom;
    }
    add_filter( 'get_the_excerpt', 'filter_function_name' );

    Plugin Author Anh Tran

    (@rilwis)

    Thank you very much for your idea and solutions! It’s nice to see how you are using the plugin in an interesting way!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Metabox field as excerpt’ is closed to new replies.