I want to display 3 related product on single page in tabs. I have made a custom custom query like this:
$postid = array(1,2,3);
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'post__in' => array_column($postid, 'ID'),
);
$loop = new WP_QUERY($args);
This query works and executes 3 times as well. But in loop I call a another template
wc_get_template_part( 'content', 'single-product-meta-side' );
In content-single-product-meta-side.php the $post variable is reset and returns only the original query post variable.
global $product, $post;
echo $post_id = get_the_ID(); // old/original post id returns
I also tried to setup_postdata( $post ); just after while loop but nothing happens.
Any ideas whats going wrong.
Another query how do I setup the global $product variable.
The function wc_get_template_part uses load_template. This last, requires the template file with some global variables available to ensure that the WordPress environment is available from within the function:
global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
So, either you use the above global variables, or you can locate the file and include it in your scope:
include(locate_template('single-product-meta-side.php'));//Don't forget to set the right path for your file.
I hope this will help you.
Related
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' );
I'm trying to make my first WP plugin, but stuck on simple function - can't get ID of post inside it.
I have tried:
$post_id = get_the_ID();
After that I thought, that my plugin works outside loop and try this:
global $post;
$post_id = $post->ID;
Few hours later I have tried to create function, that get post id after INIT:
function postidfinder () {
global $post;
$post_id = $post->ID;
return $post_id
}
add_action( 'init', 'postfinder' );
Also tried actions: wp_loaded, loop_query.
Please, help get post ID to the plugin. Thanks!
As far I know, to use post->ID outside of loop, wp_query should be called first.
global $wp_query;
$postid = $wp_query->post->ID;
Optionally, get_post_id() can work for you, check codex for more.
From the Global $post object :
Global object $post contains a lot of data of the current post. It is very easy to get ID from the object:
global $post;
echo $post->ID;
Using get_the_id() and the_id() functions:
The difference between this two functions is that get_the_id() returns ID of the current post, and the_id() prints it.
echo get_the_id();
the_id();
Is it possible to leverage WooCommerece API in a seperate, independant PHP file? For example, I want to run a php script that will tally all the sales for the day. The file will reside in the WooCommerence folder but have no connection to the WordPress or WooCommerence install:
e.g. salesTally.php
<?php
//How to include?
global $woocommerce, $post;
$order = new WC_Order($post->ID);
//Do Stuff To Order
?>
Thank you to silver.
Possible solution:
include_once('wp-load.php');
global $woocommerce;
//Search for complete wc_bookings, return a maximum of 200
$orders = get_posts( array(
'post_type' => 'wc_booking',
'post_status' => 'complete',
'posts_per_page' => 200
) );
print_r($orders);
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;
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.