wordpress page id in friendly url - wordpress

How to get the page id in wordpress in the case of friendly url?

Inside the Loop: http://codex.wordpress.org/Function_Reference/the_ID
Outside the Loop: global $wp_query; $id = $wp_query->post->ID;

I'm guessing you're talking about doing it via the Admin interface. Can't you just add it into the custom format textbox using the %post_id% string:
/archives/%post_id%

If you are within the Loop (most of the time meaning within the while(have_posts()) you should be able to use: global $post; $id = $post->ID; to get the ID

Related

How to access WordPress post admin data inside the loop?

I'm trying to access some fields that have been assigned to a post via a plugin named Snax.
You can see here in the admin that "Story" has being assigned to a post as a "Snax Format":
But when I check the $post object inside the loop on the front end using something like $my_post = get_post(); var_dump($my_post); I can't see any anything relating to snax or "story".
What php can I use to access that post data on the front end inside the loop?
I'm asking because I want to use that field data inside my post template.
I think the plugin will store they data as meta_values. You can retrieve this data with get_post_meta()
$meta_data = get_post_meta( get_the_ID() );
var_dump( $meta_data );

how to show posts data on count hyperlink in wordpress

In wordpress, I am showing a count# of posts based on various custom taxonomies. I have retrieved this count using wp-query->found_posts used in functions.php file (used tax-query). I want to have this count number as hyperlink to show details of the posts. Any suggestion on how to get this done?
I first tried to pass the WP_Query object to the php which contains the post data but obj->found_posts did not work in the php file.
You will need to capture posts' IDs from within the WP_Query you're running.
Example:
$post_ids = array();
while ( $wp_query->have_posts() ) : $wp_query->the_post();
$post_ids [] = get_the_ID() ;
endwhile;
Later on, you can use these IDs however you want.

how to retrieve custom field at root directory for wordpress?

i have create a redir.php and placed in the home directory of wordpress to do the redirect. I want to pass in the post id and then retrieve a custom field value of the post and feed into header('location:'.$url);
www.mysite.com/redir.php?id=30
in redir.php will retrieve post id=30 custom field value and pass it into $url.
this is what I have, but it's not working. It gives me "Parse error: parse error in \redir.php on line 5".
it looks like wordpress environment is not being loaded.
<?php
require('./wp-blog-header.php');
$id = $_GET['id'];
$url= get_field('deal_http_link',post->$id);
header('Location:'.$url);
?>
thanks
Your script has multiple issues:
There is whitespace before the opening <?php tag, so the redirect wouldn't work because the headers will have already been sent. Instead, <?php should be the very first thing in the file.
post->$id is invalid syntax. You probably meant the $id variable which you defined in the preceding line.
To retrieve the value of a custom field, use get_post_meta(), not get_field().
Try something like this instead:
<?php
require('./wp-blog-header.php');
$id = $_GET['id'];
$url = get_post_meta($id, 'deal_http_link', true);
header('Location: ' . $url);
exit();

Getting an author's role in Wordpress

i'm working on my first WP site and need to display an author's role next to their post. Something like "Jimmy | Administrator". Looking at the author metadata available: http://codex.wordpress.org/Function_Reference/the_author_meta doesn't give me a way to access that. I'm sure there's a quick easy way to do this and i just don't know it!! Thanks!
UPDATE: Place this in your functions.php file:
function get_author_role()
{
global $authordata;
$author_roles = $authordata->roles;
$author_role = array_shift($author_roles);
return $author_role;
}
Then call this within your Wordpress Loop. So:
<?php
if(have_posts()) : while(have_posts()) : the_post();
echo get_the_author().' | '.get_author_role();
endwhile;endif;
?>
...will print: 'Jimmy | Administrator'
COMPLETE ANSWER: The User Object itself actually stores roles, and other kinds of useful information. If you want more of a general function to retrieve the role of any given user, simply pass in the ID of the user you want to target with this function:
function get_user_role($id)
{
$user = new WP_User($id);
return array_shift($user->roles);
}
And if you want to grab the author of a given post, call it like so:
<?php
if(have_posts()) : while(have_posts()) : the_post();
$aid = get_the_author_meta('ID');
echo get_the_author().' | '.get_user_role($aid);
endwhile;endif;
?>
RESPONSE TO LAST COMMENT:
If you need to grab data outside of the Wordpress Loop (which I imagine you're trying to do on an Archive and Author page), you can use the function from my Complete answer like so:
global $post;
$aid = $post->post_author;
echo get_the_author_meta('user_nicename', $aid).' | '.get_user_role($aid);
That will output the information you want in your "user | role" format.

Add Meta Post function not working

I am using add post meta function to save some data and its not working
<?php
//include '../../../wp-blog-header.php';
$unique = "true";
$pageID = $_GET['postID'];
echo "pageID:";
echo $pageID;
echo "</br>";
$num_posts = $_GET['num_posts'];
echo "num_posts: ";
echo $num_posts;
echo "</br>";
$num_posts_meta_key = "num_posts";
add_post_meta($pageID, $num_posts_meta_key, $num_posts , $unique) or update_post_meta($pageID, "num_posts" , $num_posts);
?>
Can someone help me out?
In first page I am getting all values from textboxes or checkboxes in javascript and then i am passing it in URL to next page where add_post_meta function is there.
I tried using method POST ...but then it doesnt work for me. It just submit the page and come back w/o doing anything on 1st page. I tried with GET method..but nothing works.
Hence I decided to take all values like num of post, post id in javascript and then from there pass it with url by using window.location.
I am very new to wordpress plugin coding. I thought POST method in my plugin is conflicting with some other post method in post.php..not sure though..
I am writing plugin for admin panel.
not sure what your problem is.. are you sure you're passing the right postID parameter? does the post exist in the database?
You don't really need to do add_post_meta() or update_post_meta.
From the manual:
The first thing this function will do
is make sure that $meta_key already
exists on $post_id. If it does not,
add_post_meta($post_id, $meta_key,
$meta_value) is called instead and its
result is returned.
<?php
// This minimum code should work, though you should really check that a post
// with this id does exist.
update_post_meta($_GET['postID'], "num_posts" , $_GET['num_posts']);
?>

Resources