admin管理员组文章数量:1406950
I made an accounting system with ACF and CPT, but my client wants to be able to see the balances of each account from the post table in a column and not entering the single of each account.
My problem is that these balances are calculated with a WP_Query that is inside the single and from there is saved in the tables with an update_field. If nobody visits the single that code won't run and the field is not updated.
I just tried with an acf / save_post but although I managed to update fields in tests defining the variable myself at the time of asking you to calculate it with the query everything goes wrong and does nothing anymore.
To calculate the balance of an account I have to add and subtract all the income, expenses, transfers, purchases and sales linked to that account with an object post field that is where I take the ID for the query.
I have tried an acf / save_post but although I managed to update fields in tests defining the variable myself at the time of asking you to calculate it with the query everything goes wrong and does nothing anymore. Even if I define the variable before to run the query the function is not updating the field at least with the initial value.
To calculate the balance of an account I have to add and subtract all the income, expenses, transfers, purchases and sales linked to that account with an object post field that is where I take the ID for the query.
One of the six query's
$args = array(
'posts_per_page' => -1,
'post_type' => 'inein_ingresos',
'meta_query' => array(
array(
'key' => 'cuenta',
'value' => $CuentaID,
'compare' => '='
)
)
);
$tingresos_query = new WP_Query( $args );
if( $tingresos_query->have_posts() ):
$total_ingresos = 0;
while ( $tingresos_query->have_posts() ) : $tingresos_query->the_post();
$total_ingresos += get_field('total');
endwhile;
endif;
wp_reset_query(); // Restore global post data stomped by the_post().
The code that calculate the balances
$total_de_cuenta = 0;
require_once( 'total-ventas.php' );
require_once( 'total-ingresos.php' );
require_once( 'total-traspasos-entrantes.php' );
require_once( 'total-compras.php' );
require_once( 'total-egresos.php' );
require_once( 'total-traspasos-salientes.php' );
$total_de_cuenta = $total_ventas + $total_ingresos + $total_traspasose - $total_compras - $total_egresos - $total_traspasoss;
update_field('saldo_actual', intval($total_de_cuenta));
My save_post function
function actualizador_compras( $post_id ) {
$values = get_fields( $post_id );
$CuentaID = get_field('cuenta');
require_once( 'calculos-query/bancos/total-cuenta.php' );
}
add_action('acf/save_post', 'actualizador_compras', 15);
本文标签: custom post typesTroubles with acfsavepost and WPQuery
版权声明:本文标题:custom post types - Troubles with acfsave_post and WP_Query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744948690a2633945.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论