I have a function that returns an array for me to dynamically populate the names of inputs of a post type. So I have this array and make a loop to generate the inputs.
This function is returning the array normally in other places, but in function that I use save_post always returns NULL. So, I can not get the names to save the values with update_post_meta.
Why this function returns NULL in the function that I use save_post and other returns values normally?
Could anyone help?
Thanks ...
function retornaPresencas() {
$post_id = $_GET['post'];
if (is_null($post_id)) :
return;
else:
$datas_turma = array(); $presenca_to_check = array();
$turma_da_lista = get_field("turma_lista", $post_id);
$args = array( 'post_type' => "turmas", 'p' => $turma_da_lista );
$query_turma_lista = new WP_Query( $args );
if ($query_turma_lista->have_posts()) : while ($query_turma_lista->have_posts()) : $query_turma_lista->the_post();
if(get_field('lista_de_dias_turma')) {
while(has_sub_field('lista_de_dias_turma')) {
$datas_turma[] = get_sub_field('dia_de_aula');
}
}
$lenght_datas_turma = count($datas_turma);
$alunos_turma = get_field('lista_de_alunos_turma');
foreach ($alunos_turma as $single_aluno) :
for ($i=0; $i < $lenght_datas_turma; $i++) :
$presenca_to_check[] = $single_aluno."_".$turma_da_lista."_".$datas_turma[$i];
endfor;
endforeach;
endwhile; endif; wp_reset_query();
endif;
return $presenca_to_check;
}
Saving a post in wordpress is a POST rather than a GET. When you attach a function to the save_post action, it is passed an argument containing the post ID. Amend your function to use the provided argument when called from save_post.
Remove the if/else statement and add $post_id as a function argument.
function retornaPresencas( $post_id ) {
$datas_turma = array(); $presenca_to_check = array();
...
Related
I have a post_save hook that manages setting the title of a particular post type. Within that logic I need to retrieve the post's categories. However, the categories are not yet saved at the time the post_save triggers for the first save of a new post.
add_action('save_post', 'save_report');
function save_report($post_id) {
$data = get_post($post_id, ARRAY_A);
if($data['post_type'] == 'report') {
$date = get_post_meta($post_id)['date'][0];
// I need to get the category of the post on the first time the post is saved
// $categories only gets a value AFTER the first save
$categories = get_the_terms($post_id, 'report_type');
$cat_string = '';
foreach($categories as $value) {
$cat_string .= $value->slug;
}
$new_title = date('m/d/Y', strtotime($date)).' '.$cat_string;
remove_action('save_post', 'save_report');
wp_update_post(array(
'ID' => $post_id,
'post_title' => $new_title
));
add_action('save_post', 'save_report');
}
}
Because of this, I have to save the post twice to get it to change the title to what I want. Is it possible to get the new category of the post as it is being saved like this?
Not quite sure what you're using the categories for after the fact, but you can just retrieve them after the wp_update_post runs.
add_action('save_post', 'save_report');
function save_report($post_id) {
$data = get_post($post_id, ARRAY_A);
if($data['post_type'] == 'report') {
$date = get_post_meta($post_id)['date'][0];
$new_title = date('m/d/Y', strtotime($date)).' '.$cat_string;
remove_action('save_post', 'save_report');
/*
* return the updated post ID (which is the same really as your
* initial ID, so you could just use that. But for demonstration it helps
* to see it logically.)
*/
$updated_post_id = wp_update_post(array(
'ID' => $post_id,
'post_title' => $new_title
));
$categories = get_the_terms($updated_post_id, 'report_type');
$cat_string = '';
foreach($categories as $value) {
$cat_string .= $value->slug;
}
add_action('save_post', 'save_report');
}
}
An alternative, if you're both creating and updating posts programatically is to use wp_insert_post, which covers both new creation (if you don't pass an ID), and updating (if you do pass an ID).
You could also create a separate function to retrieve IDs that you could use with any number of other functions you run.
function retrieve_report_cats($post_id) {
cats_list = array();
$categories = get_the_terms($post_id, 'report_type');
foreach($categories as $value) {
$cat = $value->slug;
$cats_list[] = $cat;
}
return $cats_list;
}
I've searched and I could not find any solution to list a repeater field rows with Label of sub field and its value.
in my case I want to list a repeater field sub fields with Label and value.
for example :
'Sub Field Label' = 'Value'
is there any way to do this ?
If you know the labels you want to retrieve from your Repeater Field, just use the standard method:
if( have_rows('repeater_field_name') ):
while ( have_rows('repeater_field_name') ) : the_row();
echo 'Label = ' . get_sub_field('sub_field_name') . '<br>';
endwhile;
endif;
If you aren't in a single post/page or outside The Loop, just add the $post_id as the second parameter to your ACF function calls. For example: have_rows('repeater_field_name', $post_id).
If you don't know the label names, I guess you could use get_fields() to get an array of all custom fields for the current post and iterate it. Something like:
$fields = get_fields($post->ID);
foreach ($fields as $field_type => $field) {
if ( $field_type == 'repeater_field' ) {
foreach ($field as $row) {
foreach ($row as $label => $value) {
// In this case you should be aware that
// $value could be an Array too...
echo $label . ' = ' . $value;
}
}
}
}
Anyway, I recommend you to take a look at ACF Documentation. It's complete, clear and with lots of code snippets covering the most common uses.
<?php $args = array('post_type' => 'post');
$the_query = new WP_Query( $args );
query_posts( $args );
while ( have_posts() ) : the_post();
$field = get_field_object('field_name');
echo $field['label']; //print label name
echo the_field('field_name'); //and its value
endwhile; wp_reset_query(); ?>
please try this hope help to you
I hadn't realised that WordPress had added a native function for it to the Codex, but for some odd reason the page is blank. Does that mean the functionality is still coming or the page was added by mistake?
http://codex.wordpress.org/Function_Reference/get_permalink_by_slug
It's blank because it doesn't exist. If you change that function name to any random name you like you'll still see a blank page (unless of course the name you change it to is a real function).
function get_permalink_by_slug( $slug, $post_type = '' ) {
// Initialize the permalink value
$permalink = null;
// Build the arguments for WP_Query
$args = array(
'name' => $slug,
'max_num_posts' => 1
);
// If the optional argument is set, add it to the arguments array
if( '' != $post_type ) {
$args = array_merge( $args, array( 'post_type' => $post_type ) );
} // end if
// Run the query (and reset it)
$query = new WP_Query( $args );
if( $query->have_posts() ) {
$query->the_post();
$permalink = get_permalink( get_the_ID() );
} // end if
wp_reset_postdata();
return $permalink;
}
SSomeone can tell me what's the best way to get a post using it's id?
I'am using this:
$query = query_posts('post_id='.$_GET['php_post_id']);
global $post;
foreach ($query as $post):
do stuff...
This is returning an array with all post
get_post( $post_id, $output );
So in practice will look like:
$my_id = 7;
$post_id_7 = get_post($my_id);
Further reference about the post's parameters and fields, here: http://codex.wordpress.org/Function_Reference/get_post
Update: It's the best practice when you need to get a single post by id, no cicles required.
Change post_id= to p=.
$setQuery = 'p='.$_GET['php_post_id'];
query_posts($setQuery);
Click in this link to see: Retrieve a Particular Post
If you are looking to get a single post with an ID you already know or getting from another source, i'll suggest the below code.
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'p' => $id, // id of the post you want to query
);
$my_posts = new WP_Query($args);
if($my_posts->have_posts()) :
while ( $my_posts->have_posts() ) : $my_posts->the_post();
get_template_part( 'template-parts/content', 'post' ); //Your Post Content comes here
endwhile; //end the while loop
endif; // end of the loop.
You can create a query like so:
$rd_args = [
'ID' => $postId
];
$query = new WP_Query($rd_args);
And then you can retrieve the post from the query. Or set it to the global query and loop over it:
$GLOBALS['wp_query'] = $query;
while ( have_posts() ) : the_post();
Here is the code to fetch post using query_post if you know the ID.
<?php
$my_query = query_posts('post_id=111&post_type=parks'); // in place of 111 you need to give desired ID.
global $post;
foreach ($my_query as $post) {
setup_postdata($post);
the_title();
the_content();
}
?>
Im currently trying to adjust a Content SlideShow Plugin for Wordpress in order to make it compatible with WPML (Multilingual-Plugin). To achieve this, I simply need to fetch the posts from a specific category, put them into an array and return that array. WP_Query gives me a hard time doing this, as it seems like it's fetching the latest post infinite times in the loop. I'm not experienced in writing Wordpress Plugins, so I would be thankful for any hint you can give me.
This is the code of the plugins class method I'm trying to adjust.
function get_valid_posts(){
$validPosts = array();
$this_post = array();
$id_pot = array();
$my_query = new WP_Query('cat=15&showposts=10');
if($my_query->have_posts()) {
while ($my_query->have_posts()) :
$post = $my_query->post;
if(!in_array($post->ID, $id_pot)){
$this_post['id'] = $post->ID;
$this_post['post_content'] = $post->post_content;
$this_post['post_title'] = $post->post_title;
$this_post['guid'] = $post->guid;
array_push($id_pot, $post->ID);
array_push($validPosts, $this_post);
}
endwhile;
}
return $validPosts;
}
Note that I've added the $id_pot array in order to filter duplicate entries, but this shouldn't be necessary if the query / loop would work.
Thanks in advance!
I've managed to solve the problem:
function get_valid_posts(){
$validPosts = array();
$this_post = array();
$id_pot = array();
$i = 0;
$my_query = new WP_Query('category_name=gallery-post&showposts=10');
if($my_query->have_posts()) {
while($i < $my_query->post_count) :
$post = $my_query->posts;
if(!in_array($post[$i]->ID, $id_pot)){
$this_post['id'] = $post[$i]->ID;
$this_post['post_content'] = $post[$i]->post_content;
$this_post['post_title'] = $post[$i]->post_title;
$this_post['guid'] = $post[$i]->guid;
$id_pot[] = $post[$i]->ID;
array_push($validPosts, $this_post);
}
$post = '';
$i++;
endwhile;
}
return $validPosts;
}
$my_query->post returns the data of a specific post. Instead I had to use $my_query->post*s* to get an array with all the posts fetched as an object.
You are missing a call to the function the_post();:
while ($my_query->have_posts()) :
$my_query->the_post();
$post = $my_query->post;
// ...
endwhile;
See The WordPress Loop