Show only current user's posts in wordpress - wordpress

I'm trying to show posts only that the current user has created on the frontend. That is, current, logged in user should be able to view only the posts he/she created across the website, including in the blog archive I want to display only current user's posts. And anyone with a link should be able to view the post too.
I tried the code below, but this also limits access to the pages which current user didn't create. And I need to limit access only to posts.
function exclude_other_users_posts( $query ) {
if( !is_user_logged_in() ) {
// guests cannot read private posts
// and we exclude all public posts here
// so guests can read nothing :-)
$query->set( 'post_status', 'private' );
$query->set( 'perm', 'readable' );
} elseif( !current_user_can('read_others_posts') )
$query->set( 'author', get_current_user_id() );
}
add_action( 'pre_get_posts', 'exclude_other_users_posts' );

You can use something like this in the php files that you want to display your posts.
global $current_user;
get_currentuserinfo();
$authorID = $current_user->ID;
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'author' => $authorID
);
$wp_query = new WP_Query($args);
Then you can make your loop to show your posts.

Related

Automatically create a post for each user on wordpress site

I'm trying to automatically great a custom post (users) upon registering a new user to my site. I'm just barely familar with php, but I've been working off another question from stackoverflow: Automatically create a post for each user using wp_insert_post
I would ideally love to create a page upon registering or updating user information that carries over custom fields. I've used ACF to create custom fields associated with users (listed in the code as lowercase variables) and custom fields associated with the to be created custom posts (listed in the code as uppercase variables).
Thank you for any help you can offer!
function create_authors_page( $user_id ) {
$the_user = get_userdata( $user_id );
$new_user_name = $the_user->user_login;
$PostSlug = $user_id;
$PostGuid = home_url() . "/" . $PostSlug;
$member_bio = get_field('member_bio');
$contact_info = get_field('contact_info');
$member_affiliation = get_field('member_affiliation');
$my_post = array( 'post_title' => $new_user_name,
'post_type' => 'users',
'post_content' => '',
'post_status' => 'publish',
'post_theme' => 'user-profile',
'guid' => $PostGuid );
$NewPostID = wp_insert_post( $my_post );
$Member_Bio = $member_bio;
$Contact_Info = $contact_info;
$Member_Affiliation = $member_affiliation;
update_post_meta( $NewPostID, $Member_Bio, $Contact_Info, $Member_Affiliation );
return $NewPostID;
}
add_action('publish_members', 'create_authors_page');
Your hook is incorrect, use user_register action hook which fires after a user has registered and passes $user_id as a variable:
add_action('user_register', 'create_authors_page');
function create_authors_page( $user_id ) {
// do your stuff
}
You can also use profile_update hook that trigger each time user update profile.

WP_query returning all posts instead of custom post type

I'm working on a WP site and I'm having a strange problem with this WordPress query that is supposed to return the posts of custom post type osku_infopost ONLY. But instead I get ALL posts that are on the site (and pages, being of course of post_type page).
The strange part: This ONLY happens when one goes to the home URL (mysite.com). If I visit another page on the site (i.e. mysite.com/apage) the query delivers the expected results.
<?php
// fetch all infoposts in a query
$args = array(
'post_type' => 'osku_infopost',
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1,
);
$infopost_query = new WP_Query( $args ); ?>
<div class="infoposts-container">
<ul id="infoposts">
<?php while ( $infopost_query->have_posts() ) : $infopost_query->the_post(); ?>
<li class="infopost-item">
<?php echo get_the_title($infopost_query->post->ID); ?>
<?php echo get_the_content($infopost_query->post->ID); ?>
</li>
<?php endwhile; ?>
</ul>
</div>
Something seems to be injecting the query but I can't understand where or how.
I'm probably missing something very obvious here. Any ideas?
I found the Issue: I had a filter in my functions.php that I originally used because I was using the main query to retreive multiple pages (post_type page) at once.
function my_get_posts( $query ) {
if ( is_home() && false == $query->query_vars['suppress_filters'] )
$query->set( 'post_type', array( 'post', 'page') );
return $query;
}
add_filter( 'pre_get_posts', 'my_get_posts' );
When this filter is active the post_type field in the array passed to WP_query (see code in question) seems to be ignored and the query automatically returns all posts of post_type post and of post_type page as stated in the array passed to $query->set(). Thanks to everyone who looked into it!

search multiple sku's on woocommerce admin side

This sounds simple but if you have hundreds of products and need to search multiple sku's on woocommerce admin side is not available.
Lets say you need to verify 600 products, you have to:
manually add one sku to search bar
click search
get results
start again
you can't separate by , or by space, or dash.
I searched and no one have an answer or they are questions made are not answered.
How can people search multiple sku's, or products names on woocommerce admin side?
First.. you mention you have to verify many products (600), needing a multiple sku search. It looks to me you're going to do this manually?
I recommend creating a product loop in PHP where you do your verify stuff. This is probably gonna save you alot off time, and you can re-use it.
Now to the problem... Search woo products using multiple sku's.
I agree many info on the internet is kinda misleading. I've done this before, and i always used the pre_get_posts hook to change the WP (search) query before it runs.
After testing it appears the query is not being setup as a search, and the search value is empty...
So woocommerce has to do a custom search query. Luckily i quickly found mircian's post.
WooCommerce uses a Data Store for its post types ( products, orders, etc ) and the search is also done using a custom function. In this case it’s called ‘search_products’ and it does a custom query which basically returns an array of ids to be used for the results.
I modified his function to search with multiple sku's.
Insert the function in your (child) theme's functions.php.
Use '|' as a SKU delimiter. Example: '1234|1235|1236'
Tested on:
Wordpress 4.9.6
Woocommerce 3.3.5
/**
* Use multiple sku's to find WOO products in wp-admin
* NOTE: Use '|' as a sku delimiter in your search query. Example: '1234|1235|1236'
**/
function woo_multiple_sku_search( $query_vars ) {
global $typenow;
global $wpdb;
global $pagenow;
if ( 'product' === $typenow && isset( $_GET['s'] ) && 'edit.php' === $pagenow ) {
$search_term = esc_sql( sanitize_text_field( $_GET['s'] ) );
if (strpos($search_term, '|') == false) return $query_vars;
$skus = explode('|',$search_term);
$meta_query = array(
'relation' => 'OR'
);
if(is_array($skus) && $skus) {
foreach($skus as $sku) {
$meta_query[] = array(
'key' => '_sku',
'value' => $sku,
'compare' => '='
);
}
}
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'meta_query' => $meta_query
);
$posts = get_posts( $args );
if ( ! $posts ) return $query_vars;
foreach($posts as $post){
$query_vars['post__in'][] = $post->ID;
}
}
return $query_vars;
}
add_filter( 'request', 'woo_multiple_sku_search', 20 );

Restrict WordPress user to only edit posts with meta key value - WordPress admin

I have a WordPress site set up, inside it, I have created Role SubAdmin.
For posts in the admin area, I have added a meta field called "content_author".
I have restricted the listing successfully in admin area using below filter
add_filter( 'parse_query', array( $this,"addPostsAdminFilter") );
public function addPostsAdminFilter($query)
{
global $pagenow;
$type = '';
if (isset($_GET['post_type'])) {
$post_type = $_GET['post_type'];
}
if ( $post_type == $type && $pagenow=='edit.php' && !is_super_admin())
{
if ( ! $query->get( 'meta_query' ) ) //lets set meta query to filter current users listing only
{
$current_user = wp_get_current_user();
$query->set( 'meta_query', array(
array(
'key' => 'content_author',
'value' => $current_user->ID,
),
));
}
}
}
So it is limiting the list, But the problem comes here. When I have direct edit link available of any other post. I can open it easily and edit it. I don't want the user to have an access to other posts.
Can anyone help me?

Posts from category not displaying when searching for category name

I'm having trouble with my search results page in that it is not displaying posts that are a part of a category when searching for the category name. For Instance, If I search for "doors" (which is a cat) all Partners that are in the "doors" category should be displayed in the search results. Right now, only partners that have the word "doors" in their title or content is displayed.
I'm running a searchAll function so the the standard wp search will search everything.
// Define what post types to search
function searchAll( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array( 'post', 'page', 'feed', 'partner','project', 'press', 'review' ));
}
return $query;
}
// The hook needed to search ALL content
add_filter( 'the_search_query', 'searchAll' );
What am I missing?
your query is seraching for post_type, not category_name.
post_type is used for custom post types or taxonomies ..
your query should contain $query->set( 'category_name', array( 'post', 'page', 'feed', 'partner','project', 'press', 'review' ));
however, in some cases (and I do not know the reason) that would not work for sub-categories.
in that case, you should use category-slug (slug) insted.
Ive changed my string to this:
<?php
// Define what post types to search
function searchAll( $query ) {
if ( $query->is_search ) {
$query->set( 'category_name', array( 'post', 'page', 'feed', 'partner','project', 'press', 'review' ));
}
return $query;
}
// The hook needed to search ALL content
add_filter( 'pre_get_posts', 'searchAll' );
I do have custom taxonomies though. Basically I'm trying to create a "Search everything" function.

Resources