0

I generated a link on the frontend toolbar to feature a post (adding the 'featured' tag). It works OK adding or removing, but the problem is showing the post. When removing the tag and then using the_tags() or has_tags() on the template, the post still seems to have it; I have to refresh the page to see the result.

But when adding the tag, everything works as expected: the term is added and the tag is showed with a simple clic.

What am I doing wrong? Could be the action hook? Is there any type of cache?

function toolbar_add_link( $wp_admin_bar )
{
  if ( is_single() )
  {
    $title = '';
    $url = '';

    if ( has_tag( 'featured' ) )
    {
      $title = '<span style="top: 2px;" class="ab-icon dashicons-heart"></span> ' . __( 'No destacar', 'the_textdomain' );
      $url = wp_nonce_url( add_query_arg( 'the_action', 'remove_feature_post' ), 'prefix-remove_feature_post' );
    }
    else
    {
      $title = '<span style="top: 2px;" class="ab-icon dashicons-heart"></span> ' . __( 'Destacar evento', 'the_textdomain' );
      $url = wp_nonce_url( add_query_arg( 'the_action', 'add_feature_post' ), 'prefix-add_feature_post' );
    }

    $args = array(
      'id'    => 'feature-post',
      'title' => $title,
      'href'  => $url,
      'meta'  => array(
        'class' => 'dashicons-edit'
      ),
    );
    $wp_admin_bar->add_node( $args );
  }
}
add_action( 'admin_bar_menu', 'toolbar_add_link', 999 );


function add_remove_tags()
{
  global $post;

  $action = isset( $_GET['the_action'] ) ? $_GET['the_action'] : '';
  $wpnonce_action = 'prefix-' . $action;

  if ( ! ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], $wpnonce_action ) && ( current_user_can( 'editor' ) || current_user_can( 'administrator' ) ) ) )
  {
    //echo 'invalid nonce';
    return;
  }

  $term = term_exists( 'featured', 'post_tag ');
  $tag_id = null;

  if ( is_array( $term ) )
  {
    $tag_id = (int) $term['term_id'];
  }

  /*
   * If this was coming from the database or another source, we would need to make sure
   * these where integers:

  $cat_ids = array_map( 'intval', $cat_ids );
  $cat_ids = array_unique( $cat_ids );

   */

  if ( $_GET['the_action'] === 'add_feature_post' ) 
  {
    wp_add_object_terms( $post->ID, $tag_id, 'post_tag' );
  }
  elseif ( $_GET['the_action'] === 'remove_feature_post' )
  {
    wp_remove_object_terms( $post->ID, $tag_id, 'post_tag' );
  }
}
add_action( 'wp', 'add_remove_tags' );
0

Found the solution in https://core.trac.wordpress.org/browser/tags/4.0/src/wp-includes/taxonomy.php#L0

wp_add_object_terms() uses wp_cache_delete(), but wp_remove_object_terms() don't. Just added this:

wp_remove_object_terms( $post->ID, $tag_id, 'post_tag' );
wp_cache_delete( $object_id, 'post_tag_relationships' );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.