Would like to change and update a post category
using wp_update_post
. The category should be changed by clicking <a>
or <button>
As far as I can see, this should apply the update to the post;
$live_paused = array(
'post_category' => 6
);
// Update the post into the database
wp_update_post( $live_paused );
But how can I turn add this function to this
echo '<a href="" id=""><i class="fa fa-pause"></i></a>';
Edit - further information
Update Post Code in functions of theme - not tested yet.
function live_paused_status( $post_id ){
if (current_user_can('edit_post', $post->ID)) {
$live_paused = array(
'post_category' => 6
);
echo '<a href="" id=""><button title="" data-toggle="tooltip" class="campaigns-link-button" type="button" data-original-title="Pause Campaign"><i class="fa fa-pause"></i></button></a>';
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'live_paused_status');
// update the post, which calls save_post again
wp_update_post( $live_paused );
// re-hook this function
add_action('save_post', 'live_paused_status');
}
}
add_action('save_post', 'live_paused_status');
Loop
<?php $query = new WP_Query( array( 'post_type' => 'campaigns'));?>
<?php if ($query->have_posts()) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="card">
<div class="card-footer">
<div class="row">
<div class="col-4 campaigns-link">
<?php echo live_paused_status(); ?>
</div>
</div>
</div>
</div>
<?php endwhile; wp_reset_postdata(); ?>