Wordpress, global post changed after query - php

I am developing a plugin. In this plugin, I've created a custom post type 'toto'.
In the admin page, I edit a custom post element with the following post id '82'.
In this page, I launch a query to retrieve elements with another post_type like that :
$featured_args = array(
'post_type' => 'other_type',
'post_status' => 'publish'
);
// The Featured Posts query.
$featured = new WP_Query( $featured_args );
// Proceed only if published posts with thumbnails exist
if ( $featured->have_posts() ) {
while ( $featured->have_posts() ) {
$featured->the_post();
if ( has_post_thumbnail( $featured->post->ID ) ) {
/// do stuff here
}
}
// Reset the post data
wp_reset_postdata();
}
Doing that the global $post changed. It is not the post with the id 82 anymore but the latest post element from the query.
I thought that the wp_reset_postdata() function will allow me to retrieve my current $post. I also tried with wp_reset_query() without changes.
Am I missing something?
Any help would be appreciated.

wp_reset_postdata() resets the main loop. If you want to access $post directly try
global $post;
$backup_post = $post;
//do another loop
wp_reset_postdata();
$post = $backup_post;

Related

How to move all the posts to move to trash

I'm facing the issue in route change by click the apply button, while selecting the move to trash.
NOTE: move to trash and apply process is working by selecting some posts. Bulkly, I trying to move to trash is facing the issue.
url : https://abcd.com/blog/probs/wp-admin/edit.php
After selecting move to trash without selecting any posts and click the apply button url changes to
Incase of delete all posts, I got a route like this
changed url : https://abcd.com/probs//wp-admin/edit.php?paged=1
For this you can try it programmatically by creating your code.
for trashing all the post you try this code:
function move_all_posts_to_trash() {
$args = array(
'post_type' => 'post', // Change this to the post type you want to move to trash
'posts_per_page' => -1, // Get all posts
'post_status' => 'publish' // Only get posts that are published
);
$posts = get_posts( $args );
foreach ( $posts as $post ) {
wp_trash_post( $post->ID ); // Move post to trash
}
}
// If you want to move single post or page you can used by default wordpress function
Basic Example
Trash the default WordPress Post, “Hello World,” which has an ID of ‘1’.
<?php wp_trash_post( $post_id = 1 ); ?>
// For Multiple Posts using WP_Query using while loop.
You can used as per requirement like create shortcode or fire hooks.
function trash_custom_posts() {
$args = array (
'post_type' => 'custom_post_type_slug',
'post_status' => 'publish',
'posts_per_page' => -1
);
$query = new WP_Query( $args );
while( $query->have_posts() ) {
$query->the_post();
$post_id = get_the_ID();
wp_trash_post( $post_id );
}
wp_reset_postdata();
}
add_action( 'init', 'trash_custom_posts' );

looping through pages and updating ACF field data for each inside of a function

Having a bit of an issue and hoping for some help. Basically I am triggering a function on acf/save_post action for a custom post type called 'settings'. So if 'settings' is saved or updated I want to loop through all existing pages and update a ACF field for each page with a specific piece of data. My loop is below. I've been fighting with this thing all weekend but it doesn't seem to update any pages. Any help would be huge! Thanks!
$the_query = new WP_Query( array( 'post_type' => 'page' ) );
while ( $the_query->have_posts() ) : $the_query->the_post();
global $post;
$pageID = $post->ID;
update_field('temp_post_data','test' , $pageID);
endwhile;
You are trying to update field on current page.
Try this
function my_custom_function() {
$pages = get_pages(); // get all pages
foreach($pages as $page):
// error_log($page->ID); // debug if needed
update_field('temp_post_data','test' , $page->ID); //for each page we are updating field
endforeach;
}

Displaying content of Advanced Custom Fields

I’m having a little problem understanding what (my) PHP is doing when I’m trying to display different Custom Posts and Advanced Custom fields on the same page.
I have added different Advanced Custom Fields to a page, and I have custom posts that I am trying to display using the template.
I’m calling my custom fields throughout the template using:
<?php the_field(‘field-name’) ?>
My custom posts are called through loops like this (somewhere around the middle of the template):
<?php
$args = array(
'post_type' => ‘foo’
);
$foo = new WP_Query( $args );
if( $foo->have_posts() ) {
while( $foo->have_posts() ) {
$foo->the_post();
?>
<?php the_content() ?>
<?php
}
}
else {
// SOME MESSAGE
}
?>
The content of the Advanced Custom Fields is showing fine, above those loops. Below the loops it just doesn’t display.
I can’t figure out why the content is not showing up.
I assume it has to do with the while or if statements of the loops. If I remove the loops, the content of any Advanced Custom Field below does display.
When you use WP_Query(), you are changing the default $post variable on the page each time you loop through a post. You need to call wp_reset_postdata() after your loop to reset that $post variable so it corresponds to the current page again. You can call the function after your 'while' loop -
<?php
$args = array(
'post_type' => ‘foo’
);
$foo = new WP_Query( $args );
if( $foo->have_posts() ) {
while( $foo->have_posts() ) { $foo->the_post();
the_content();
} wp_reset_postdata();
}
else {
// SOME MESSAGE
}
?>

Display latest articles inside of a post on wordpress

i was browsing some themes on wordpress, and i found one that is kind of a clone from 9gag.tv
You can see the demo theme here
http://codecanyon.net/item/youtube-viral-videos-9gag-tv-clone/full_screen_preview/6770578
If you see that page, you see the "latest videos" area below, that has the posts arranged by date, but when you open a video, that area changes and it gives you random videos.
Is there a way to arrange the videos like in the home page and allow user to browse to older videos?
I guess its easy to achieve it using the get_posts() loop. You'll also need to register new custom post_type for whatever posts you're going to post and fetch it through a loop like the example below.
I'm sure someone else will give a full answer but this will get you started.
http://codex.wordpress.org/Post_Types
http://codex.wordpress.org/Template_Tags/get_posts
<?php
$args = array( 'post_type' => 'custom_post_type', 'posts_per_page' => 10, 'orderby' => 'rand' );
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $post ) {
setup_postdata( $post );
the_attachment_link( $post->ID, false );
the_content();
}
wp_reset_postdata();
}
?>
You can use wordpress php function get_posts() to fetch the posts, then echo the data you want using a foreach. The function also has the offset argument which you can use for the navigation.

Multiple loop issue - pull 1 featured and continue the loop without it

I am writing a custom multiple loop to be used on a custom category template page. The loop should put one post that is checked as featured in admin, in a separate div, and continue the loop displaying all posts from the category except the featured.
Similar to the example provided on the codex page except I don't want to create a separate category for the featured post.
I am using Advanced Custom Fields plugin for the check box that sets posts as featured.
I have the following issue with my code: if ($post->ID == $do_not_duplicate) continue; prevents rest of the loop to be executed. The code below just pulls the latest featured post.
Here is my function:
function featured() {
$featured = new WP_Query(array(
'meta_query' => array(
array(
'key' => 'featured',
'value' => '"top"',
'compare' => 'LIKE'
)
),
'posts_per_page' => 1
));
while ( $featured->have_posts() ) : $featured -> the_post();
$do_not_duplicate = $post->ID; ?>
<div id="featured">
//featured post
</div><!-- end #featured -->
<?php
endwhile;
if(have_posts()) : while (have_posts()) : the_post();
if ($post->ID == $do_not_duplicate) continue;
?>
<div class="container">
// normal posts
</div><!-- .charities-container -->
<?php
endwhile;
endif;
}
Your fresh eyes will help a lot!
Thanks!
Looks like your $post variable isn't being assigned with the current post information through the loop. As far as I can tell, you could try either one of these ways:
1) Global $post
The $post variable is within your featured() function scope. Therefore, when you run your loop, it is not being recognized as the global $post variable, which is the one WordPress fills with post information through the loop. Simply declare $post as a global variable at the beginning of your function scope and you should be able to gather post information:
function featured() {
global $post;
// ... all your function code, $post->ID should work now
}
or 2) Use get_the_ID()
You could replace $post->ID for WP's native function get_the_ID(). This is just about the same as the previous solution, as this function will retrieve the ID property from the global $post object automatically. I would take this as the best solution, as you don't have to worry about scope whenever using post functions (get_the_ID(), get_the_title(), etc) as long as the $post object is populated (after the_post() is called).
So you could replace this line:
$do_not_duplicate = $post->ID;
for
$do_not_duplicate = get_the_ID();
and
if ($post->ID == $do_not_duplicate) continue;
for
if (get_the_ID() == $do_not_duplicate) continue;
Try either one of these solutions, I bet both should work for you. Actually, the example you took from codex page works just fine, the problem is that you applied it inside a local function. This way your $post variable is a local (function scope) variable, and not a global one.

Categories