I am making a plugin for wordpress. And the problem is that, how can I get the current post ID outside the loop?
I have a button on the post, and the post ID should be returned when the button is clicked by the user.
So, do anyway to get the post id?
My plugin dir: wp-content\plugins\updateDatabase\js\connectionDatabase.php
Here is my code: And my url http://localhost:8080/wordpress/?p=1169
$post_id = $_GET['p'];
echo $post_id;
$link = getConnection();
$bonusPoint = 0;
$query_sql = "SELECT bonus_point FROM post_data03 WHERE post_id =" .$post_id;
$result = mysqli_query($link, $query_sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$bonusPoint = $row['bonus_point'];
}
} else {
echo "0 results";
}
Using $post:
function getBonusPointFromDataPost()
{
global $wp_query;
global $post;
$post_id = add_action( 'the_post', 'get_post_info' );
$link = getConnection();
}
GetBonusPointFromDataPost() will be fired when the user clicks the button which on the post page. And at the same time, the $post_id should be returned.
And I make a function to return the $post_id, but it only work when I add the action to it.
function get_post_info() {
global $wp_query;
global $post;
$post_id = $wp_query->post->ID;
echo $post_id;
return $post_id;
}
How can I return the $post_id when the user clicks the button.
Here it is,
use global $post to get the current post object
global $post;
echo $post->ID;
Related
I am building a website that able user to posts an article using wordpress (I build my own theme). And in home page, the user will view posts by user that followed by it (like twitter timeline). I use infinite scroll based on jQuery to replace the pagination.
Everything is fine and works if I login as admin, the problem is when I login as another privilege such as author. The page will only load first page. And another page will not loaded.
This is my code in function.php :
//-----------------Infinite Scroll-------------------------------
/*
* initial posts dispaly
*/
function script_load_more($args = array()) {
//initial posts load
echo '<div id="ajax-primary" class="content-area">';
echo '<div id="ajax-content" class="content-area">';
ajax_script_load_more($args);
echo '</div>';
echo '<center><a href="#" id="loadMore" data-page="1" data-url="'.admin_url("admin-ajax.php").'" ></a></center>';
echo '</div>';
}
/*
* create short code.
*/
add_shortcode('ajax_posts', 'script_load_more');
/*
* load more script call back
*/
function ajax_script_load_more($args) {
//init ajax
$ajax = false;
//check ajax call or not
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$ajax = true;
}
global $wpdb;
$cekpost = $wpdb->get_results ( "SELECT post_author FROM ".$wpdb->prefix."posts WHERE post_author = ".get_current_user_id()." AND post_status='publish'");
$result = $wpdb->get_results ( "SELECT follow FROM ".$wpdb->prefix."follow WHERE user = ".get_current_user_id());
if (sizeof($cekpost)>0 OR sizeof($result)>0){
//number of posts per page default
$num =5;
//page number
$paged = $_POST['page'] + 1;
$jumlah=count($result);;
$i=0;
$following = get_current_user_id();
foreach ( $result as $hasil )
{
$i++;
//if ($i<$jumlah){
$following=$following.",".$hasil->follow;
//$following=$following.",";
//}
}
//echo $following;
if ($following!=null){
echo "inside following".get_current_user_id();
//args
$args = array(
'post_type' => 'post',
//'author' => $following,
'post_status' => 'publish',
'posts_per_page' =>$num,
'paged'=>$paged
);
//query
$query = new WP_Query($args);
//var_dump($query);
//check
if ($query->have_posts()):
//loop articles
while ($query->have_posts()): $query->the_post();
include 'ajax-content.php';
//reset post data
wp_reset_postdata();
endwhile;
endif;
}
//check ajax call
if($ajax) die();
} else {
include_once "postuser.php";
}
}
Can you please check if any js errors in console. May be cache or optimisation plugins issues
Then I realized that the problem is when we call ajax in wordpress it always fire on is_admin. So wherever you try to use ajax it always applied like on admin side.
I need in the act of publishing a post, a value insert in a custom_field, the same post_title value!
function add_custom_field_automatically_two($post_ID) {
global $post;
$post_id = $post->ID;
global $wpdb;
//$my_customf = $post->post_title;
//$my_customf = get_the_title( $post_id );
$my_customf = "something string"; // worked, but I need post_title!
add_post_meta($post_ID, 'my_customf', $my_customf, true);
}
add_action('publish_page', 'add_custom_field_automatically_two');
add_action('publish_post', 'add_custom_field_automatically_two');
?>
Thanks.
I wan't to restrict certain product pages from logged-out users or users with a specific role. The easiest way would probably be to check the category ID of the product page and if current_user_can('') than redirect to the main shop page.
However I don't really know where to start.. Should I add an action on init? And how do I check for the current page product ID?
I thought I could get some data with a var_dump() But that resulted in nothing.
I did this:
add_action('init', 'get_all_post_meta');
function get_all_post_meta() {
//$meta = get_post_meta( get_the_ID() );
global $post;
var_dump('$post');
$metavar = get_the_terms($post->ID);
var_dump('$metavar');
}
But no results in my console.
Edit: I found my var_dump() was incorrect as it should be like var_dump($post); Continuing my quest now.
This is my solution so far, now I need to figure out how to remove a single product instead of them all.
add_action('wp_head', 'get_all_post_meta', 1 );
function get_all_post_meta() {
global $post;
$banned_cid = array(8);
$current_cid = array();
$metavar = get_the_terms($post->ID, 'product_cat');
global $woocommerce;
$redirect_url = 'http://www.example.nl/';
if(current_user_can('subscriber') || current_user_can('manage_options')){}else if( is_product()){
foreach ( $metavar as $term ) {
$cat_id .= $term->term_id.',';
array_push($current_cid, $term->term_id);
}
var_dump($current_cid);
$c = array_intersect($banned_cid, $current_cid);
if (count($c) > 0) {
$woocommerce->cart->empty_cart();
wp_redirect( $redirect_url, 302 );
exit;
}
}
}
function awepop_show_views($singular = "view", $plural = "views", $before = "This post has: ")
{
global $post;
$current_views = awepop_get_view_count();
$views_text = $before . $current_views . " ";
if ($current_views == 1) {
$views_text .= $singular;
}
else {
$views_text .= $plural;
}
return $views_text;
}
function awepop_append_to_meta($meta){
return $meta[] = awepop_show_views();
}
add_filter( 'the_meta_key', 'awepop_append_to_meta' );
I am trying to embed post views into post meta. I search alot but couldn't find the appropriate filter for post meta. please advice how can i embed my view count into post meta
Try hooking to init and using update_post_meta. For example, this is how you could increment your post views:
function my_update_postmeta() {
global $post;
update_post_meta( $post->ID, my_meta_key, $my_meta_value + 1 );
}
add_action( 'init', 'my_update_postmeta' );
Note: You would put my_update_postmeta() in your page or post template.
Ref: http://codex.wordpress.org/Function_Reference/update_post_meta
I want to write a custom next/prev function to dynamically display post information in a fancybox pop-up. So I need to use PHP to get the next and previous Post ID based on whatever Post is currently showing. I know what the current post ID is and I can send that to a function ok, but I can't figure out how to use that ID to obtain the adjacent IDs.
Edit:Here is my code so far (which is not working)
<?php
require_once("../../../wp-blog-header.php");
if (isset($_POST['data'])){
$post_id = $_POST['data'];
}else{
$post_id = "";
}
$wp_query->is_single = true;
$this_post = get_post($post_id);
$in_same_cat = false;
$excluded_categories = '';
$previous = false;
$next_post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);
$post_id = $next_post->id;
$title = $next_post->post_title;
$dataset = array ( "postid"=>$post_id, "posttitle"=>$title );
//Because we want to use json, we have to place things in an array and encode it for json.
//This will give us a nice javascript object on the front side.
echo json_encode($dataset);
?>
get_adjacent_post() uses the global $post as its reference point, so you'll want to replace this:
$this_post = get_post($post_id);
with this:
global $post;
$post = get_post($post_id);
WordPress also provides get_next_post() and get_previous_post(), which you can use here instead of using get_adjacent_post() with all of those arguments. Here's the final product:
<?php
require_once("../../../wp-blog-header.php");
if (isset($_POST['data'])){
$post_id = $_POST['data'];
}else{
$post_id = "";
}
$wp_query->is_single = true;
global $post;
$post = get_post($post_id);
$previous_post = get_previous_post();
$next_post = get_next_post();
$post_id = $next_post->id;
$title = $next_post->post_title;
$dataset = array ( "postid"=>$post_id, "posttitle"=>$title );
//Because we want to use json, we have to place things in an array and encode it for json.
//This will give us a nice javascript object on the front side.
echo json_encode($dataset);
?>
I'm not sure what keys you'd like to use for the IDs and titles of the previous and next posts in the $dataset array, so I'll leave that as is for now.
To get this to work I had to change $next_post->id; to $next_post->ID;, i.e. capitalise ID.
I used it in Wordpress on index.php like this – I inserted this at the top of the loop:
<div class="work-post" id="<?php the_ID(); ?>">
then within the loop I use this:
`
if (isset($_POST['data'])){
$post_id = $_POST['data'];
}else{
$post_id = "";
}
$wp_query->is_single = true;
global $post;
$post = get_post($post_id);
$next_post = get_next_post();
$previous_post = get_previous_post();
?>
<!-- call only if a value exists -->
<?php if($next_post) : ?>
<div>PREV.</div>
<?php endif; ?>
<!-- call only if a value exists -->
<!-- call only if a value exists -->
<?php if($previous_post) : ?>
<div>NEXT</div>
<?php endif; ?>
<!-- call only if a value exists -->`
This is my code, it worked good. $post_id is current post ID.
function get_previous_post_id( $post_id ) {
// Get a global post reference since get_adjacent_post() references it
global $post;
// Store the existing post object for later so we don't lose it
$oldGlobal = $post;
// Get the post object for the specified post and place it in the global variable
$post = get_post( $post_id );
// Get the post object for the previous post
$previous_post = get_previous_post();
// Reset our global object
$post = $oldGlobal;
if ( '' == $previous_post )
return 0;
return $previous_post->ID;
}
function get_next_post_id( $post_id ) {
// Get a global post reference since get_adjacent_post() references it
global $post;
// Store the existing post object for later so we don't lose it
$oldGlobal = $post;
// Get the post object for the specified post and place it in the global variable
$post = get_post( $post_id );
// Get the post object for the next post
$next_post = get_next_post();
// Reset our global object
$post = $oldGlobal;
if ( '' == $next_post )
return 0;
return $next_post->ID;
}
Then you just call function. Example:
echo get_previous_post_id( $current_id );
echo get_next_post_id( $current_id );
Good luck!