Skip to content

Instantly share code, notes, and snippets.

@russo97
Last active May 20, 2022 13:48
Show Gist options
  • Save russo97/72ce512306eb580d706eb9083418711d to your computer and use it in GitHub Desktop.
Save russo97/72ce512306eb580d706eb9083418711d to your computer and use it in GitHub Desktop.
Expose CPT data in WordPress
<?php
function exibir_pesquisas () {
$args = array(
'post_type' => 'desenvolvimento', 'posts_per_page' => -1
);
$wp_query = new WP_Query($args);
if ($wp_query->have_posts()) {
$pesquisas = [];
header("Content-type: application/json");
foreach ($wp_query -> get_posts() as $pesquisa) {
$pesquisas[] = [
"titulo" => $pesquisa -> post_title,
"conteudo" => $pesquisa -> post_content,
"situacao" => get_field("situacao", $pesquisa -> ID),
"prazo_de_execucao" => get_field("prazo_de_execucao", $pesquisa -> ID),
"investimento_realizado" => get_field("investimento_realizado", $pesquisa -> ID),
"objetivo" => get_field("objetivo", $pesquisa -> ID),
"entidades_envolvidas" => get_field("entidades_envolvidas", $pesquisa -> ID),
];
}
echo json_encode([$pesquisas]);
} else {
echo 402;
}
exit;
die();
}
add_action('wp_ajax_nopriv_exibir_pesquisas', 'exibir_pesquisas');
add_action('wp_ajax_exibir_pesquisas', 'exibir_pesquisas');
// appears in /wp-admin/admin-ajax.php?action=exibir_pesquisas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment