wordpress: want to show only admin posts on home page - wordpress

Suppose i have a blog where lots of people post their post under different category. But i want to show only those post on home page which are posted by a admin. Rest of the author post will be shown under specific users pages.
So is there any plugins available for this? or any code that might help me to fullfil my purpose.

$query = new WP_Query( 'author=1' );
userid 1 is always admin, if you havent changed it, if you have multiple admins please have it : $query = new WP_Query( 'author=2,6,17,38' );
You can read more about it here: http://codex.wordpress.org/Class_Reference/WP_Query
Good luck!
Marten

Related

fetch registered user posts in wordpress

I am making a wordpress website on which the main posts are coming on the homepage .
I want that if a user regiters on the website and if he make posts on the website , his/her post should go on a a specific page rather than coming on the homepage where the main admin posts are coming .
how to fetch the registered user posts to a specific page .
Please help if anyone can .
Thanks in advance
You can try specifying the author ID. For content-home.php page use
$posts = get_posts('author=1&posts_per_page=1&orderby=date&order=desc');
$post = $posts[0];
If you want to show only the latest post.
And for the other page exclude this ID.
$posts = get_posts('author=-1');

Wordpress secure way to have Private/Public posts

I've asked a few questions trying to solve this simple problem, but nothing seems to work.
Whats the recommended way to have private/public posts? I want to have a site that if an author/editor/administrator are logged in every private post and public post are viewable/searchable. If the user is not logged in only public posts a viewable.
I have thought about/tried doing this a number of ways. A simple way I achieved this way using a WP_Query to include/excluded all posts with a custom field "Private" when logged in/out.
While this worked fine I have two problems with it, how secure is it? and It requires a custom field, when Wordpress already has private post functionality.
The other way I have tried is to use Wordpress built in Private post feature but I cant get the private post to show up on the front-end. They show up in the edit screen for allowed users and in the loop(front-end) for admins but not editors or authors....
Using wordpress built in functions is my perferrred method but just cant get it to work correctly.
any suggestions or help? Someone must have done this without the need for a custom field?
thanks
You dont need to use a meta field to get private posts, its available on the wp query post_status parameter.
$args = array( 'post_status' => array( 'publish' ) ); // regular users
if ( is_user_logged_in() ) {
// signed in users
$args['post_status'][] = 'private';
}
$query = new WP_Query( $args);
I believe the most appropriate in your case is to use WordPress capabilities. Editors are already able to view private posts/pages on the front-end if logged in (because they have the read_private_posts capability).
Here's an example of how you would make private posts/pages viewable by author user role.
function so0805_init_theme_add_capabilities(){
/* allow authors to view private posts and pages */
$role_author = get_role('author');
$role_author->add_cap('read_private_pages');
$role_author->add_cap('read_private_posts');
}
add_action('init', 'so0805_init_theme_add_capabilities');
Paste this code inside functions.php of your theme.

Wordpress - remove some posts from query

I'm trying to make a plugin.
Its job is to generate and send a link to the author after a post is published by admin.
After clicking on the link will be the post actually published.
I did that after the click on the link there will be a post meta added to the post.
NOW I cant find a solution how to show only posts with the meta or ADMINS (or with some user level) posts.
I decided I need a filter bud I cant figure out how to do the ADMIN posts exceptions.
How do I filter only non-admin posts.
I think I need to remove the "bad" posts from $query but how ?
add_filter( 'pre_get_posts' , 'postsClean' );
function postsClean( $query ){
// check all posts and if the post should be not published remove it from query
}
Or is there any better way ?
If you are using wp_query you can use - for negation, e.g.
$wp_query_obj->set( 'author', '-1' );
WP_Query shows a full list of query arguments.

Wordpress permalink from the database?

I am trying to access selected post information from my WordPress database to display on a second website which is not a WordPress site nor is it php, it is in fact an asp.net website that i want to display the information on.
I have a WordPress blog at blog.domain.com and another website at domain.com both running on different servers and I need to display the excerpt, title with link on my asp.net website for selected posts. I can easily grab the excerpt and title from the blog's database but the only problem is that the permalink is not in the database.
I had the idea that maybe if I place a short piece of code on every blog to insert the permalink to a new table, i could then grab that to use over on the other website. But that didnt work, perhaps I am not doing it right. This is the code I used;
<?php
global $wpdb;
$table_name = $wpdb->prefix . "posts_pages_url";
$wpdb->insert($table_name , array('post_id' => the_ID(), 'url' => the_permalink()));
?>
Some might say, "why dont you just use an RSS Feed?" Well if it comes down to it, I just might have to, but i would prefer not to as connecting to the database directly to access my selected posts is much more flexible.
I would be very grateful if anyone had any suggestions on how I can access the permalink via a database call.
Many thanks
You could use get_permalink() in your code, or link to the guid value in the DB - not ideal I know, but it should at least work.
EDIT:
Same goes for the_ID() - it should be changed to get_the_ID().
This code will get it for you if using Yoast
$q = "SELECT * FROM wp_yoast_indexable yi
WHERE object_type = 'post'
AND object_id = '$ref'";
$yis = $scDb->select($q);
$permaLink = "";
if(isset($yis[0]))
{
$permaLink = $yis[0]->permalink;
}

Allow only certains user posts to display on a page in Wordpress?

I have two special users on my site. Let's call them user1 and user2. User 1 manages "user page 1" and user2 manages "user page 2". I want their posts to appear when I click on these links and only see their posts in Wordpress.
Should I use categories for this and if so, how do I create a link to only show posts from a certain category?
Do I need a module for this?
If the answer is still not clear, how do I do this?
You can use wp_qurey() function to show posts from specific authors.
Display posts by author, using author id:
$query = new WP_Query( 'author=123' );
Display posts by author, using author 'user_nicename':
$query = new WP_Query( 'author_name=rami' );
More details here - http://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
You can use the get_author_posts_url() to create a link to a page listing a specific author's posts: http://codex.wordpress.org/Function_Reference/get_author_posts_url
The format of the link (if you just want to go ahead and test it in your browser first) will be http://www.yoursite.com/author/authorname.
Hope that helps!
Copy your index page loop and then change query from post_per_page query to this and you're done.
$query = new WP_Query( 'author_name=rami' );

Resources