Allow current user to navigate only his own posts in wordpress - wordpress

I am using a custom plugin to add previous and next links in the "admin edit post" page. This so that the author can easily skip from his own posts (previous to next) without having to leave that page in the backend.
I am however struggling with how I need to get there.
Is the below adjustable to make it display only the current editing user ?
function change_apn_post_status( $post_statuses, $post_type ) {
// Add a post status.
// Note: by default these are already in the $post_statuses array: 'draft', 'future', 'pending', 'private', 'publish'
$post_statuses[] = 'trash';
// Remove post status(es).
$post_statuses_to_remove = array( 'draft' ); // Customize here.
if ( 'page' === $post_type ) {
$post_statuses_to_remove[] = 'pending';
}
foreach ( $post_statuses_to_remove as $remove ) {
if ( false !== $index = array_search( $remove, $post_statuses ) ) {
unset( $post_statuses[ $index ] );
}
}
return array_values( $post_statuses );
}
add_filter( 'c2c_admin_post_navigation_post_statuses', 'change_apn_post_status', 10, 2 );'
I thought this was a good starting point but alas I can't get it to work
global $current_user;
wp_get_current_user();
$author_query = array(
'posts_per_page' => '-1',
'author' => $current_user->ID
);

Related

I want to only display orders that belong to a certain role in User Role Editor Pro on the woocommerce orders page

We are using User Role Editor Pro to manage user roles. We want to only display orders belonging to the respective "salesagent" (role).
I have been able to pull in the current user role and get access to the order columns on the correct page. I do not see any role information on the order: https://gist.github.com/leafdropco/2ae7281cc81864e74af28c507f3c2ad0
How can i only show orders to the correct agents?
// Hide products from agents that dont own them
add_action( 'manage_shop_order_posts_custom_column', 'display_sales_agent_orders_only' );
function display_sales_agent_orders_only( $columns ) {
global $the_order, $post;
if( is_user_logged_in() ) { // check if there is a logged in user
$user = wp_get_current_user(); // getting & setting the current user
$roles = ( array ) $user->roles; // obtaining the role
if(in_array('sales_agent', $roles)) {
$salesagentid = '60802fa984f32';
if ( $salesagentid === $columns ) {
print_r($columns);
}
}
}
}
To get the customer from an order, you can do:
1. Woocommerce ways
wc_get_orders and user_id parameter (see)
$args = array(
'customer_id' => $user_id
);
$orders = wc_get_orders($args);
2. WordPress way
With a custom wp_query on the Order post, and _customer_user post meta
$customer = wp_get_current_user();
// Get all customer orders
$customer_orders = get_posts(array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'orderby' => 'date',
'order' => 'DESC',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types(),
'post_status' => array_keys(wc_get_order_statuses()),
));
3 - get the user from an order Object
If you want to retrieve the user_id of an order, see $order->get_user() or $order->get_user_id() (see)
Here is what worked for me:
add_action( 'pre_get_posts', 'admin_pre_get_posts_orders_query', 9999 );
function admin_pre_get_posts_orders_query( $query ) {
global $pagenow;
if (current_user_can('edit_others_shop_orders')) { return $query; }
// Targeting admin orders list
if( 'edit.php' == $pagenow &&
$query->get( 'post_type' ) == 'shop_order'
) {
$query->set( 'meta_query', array( array(
'key' => 'wcb2bsa_sales_agent',
'value' => get_current_user_id() ) ) ); // Only displays the orders created by the current user
}
}

Customizing Woocommerce Loop Query

I want to display my woocommerce products on two different sections on the site i.e. the shop page and on an archive page i created using Custom Post UI plugin, called artists.
Using the Advanced Custom Fields plugin, i created a field that will attach each product created to an individual artist. The plan now is, on each artist page, pull products for the artist.
This is what i have so far. I hook into woocommerce_product_query, check if i'm on the single artist page and implement my meta query:
function artist_products( $query )
{
if( is_singular('artists') )
{
$meta_query = ( is_array( $query->get('meta_query') ) ) ? $query->get('meta_query') : [];
$meta_query[] = array(
'key' => '_fld_select_artist',
'value' => get_the_ID(),
);
$query->set( 'meta_query', $meta_query );
}
}
add_action( 'woocommerce_product_query', 'artist_products' );
This is not working. When i visit my single artist page, i get a 500 server error. What I'm i missing?
woocommerce_product_query was breaking my site for some reason so i switched to pre_get_posts:
function artist_products( $query )
{
if ( $query->get('post_type') == 'nav_menu_item' )
{
return $query;
}
if( ! is_admin() && is_singular('artists') )
{
$meta_query = ( is_array( $query->get('meta_query') ) ) ? $query->get('meta_query') : [];
$meta_query[] = array(
'key' => '_fld_select_artist',
'value' => get_the_ID(),
'compare' => '=',
);
$query->set( 'meta_query', $meta_query );
}
}
add_action( 'pre_get_posts', 'artist_products' );

How to Populate a Drop-down field in WP

I have a gravity form form on my WP site and I recently changed a free text field into a drop down field.
The website is a store which hold several categories of goods and I want my drop-down to show the user all the possible categories he can choose from.
Please assist in how to "pull" the categories into the drop-down list.
Thanks in advance.
You can do using some filters of gravity form, code is following
// Here 1 is form id
add_filter( 'gform_pre_render_1', 'populate_category' );
add_filter( 'gform_pre_validation_1', 'populate_category' );
add_filter( 'gform_pre_submission_filter_1', 'populate_category' );
add_filter( 'gform_admin_pre_render_1', 'populate_category' );
function populate_category( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-category' ) === false ) {
continue;
}
// Get category list
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
$choices = array();
foreach( $categories as $category ) {
$choices[] = array( 'text' => $category->name, 'value' => $category->name );
}
$field->placeholder = 'Select a Category';
$field->choices = $choices;
}
return $form;
}
This is working perfectly its tested code.

Set custom post default language

Set custom post default language.
I created a custom post with pods and I have WPML plugin installed.
I want to show this custom post only in a language which is not the default one of the website so that the urls will be http://example.com/en/postname instead of http://example.com/postname.
For this I would like when i create a new post of these custom posts that their default language should be english and not the default language of the website.
Otherwise I have every time to change the default language of the post for each post.
How can i set another default language for the custom post than the one of the website?
add_action('save_post', 'my_english_halacha');
function my_english_halacha($post_id) {
$post_type = get_post_type($post_id);
switch( $post_type )
{
case 'english_halacha':
$set_language_args = array(
'element_id' => $post_id,
'element_type' => 'post_english_halacha',
'language_code' => 'en',
'rewrite' => array('slug' => ( (ICL_LANGUAGE_CODE=='en') ) ),
'source_language_code' => 'he',
);
global $sitepress;
$sitepress->switch_lang('en');
do_action( 'wpml_set_element_language_details', $set_language_args );
break;
case 'spanish_halacha':
$set_language_args = array(
'element_id' => $post_id,
'element_type' => 'post_spanish_halacha',
'language_code' => 'es',
'rewrite' => array('slug' => ( (ICL_LANGUAGE_CODE=='es') ) ),
'source_language_code' => 'he',
);
global $sitepress;
$sitepress->switch_lang('es');
do_action( 'wpml_set_element_language_details', $set_language_args );
break;
}
}
Add the following action to functions.php, and I hope Problem will be Solved:
function update_post_language( $post_id ) {
$post_type = get_post_type($post_id);
if ($post_type == 'dwqa-question' || $post_type == 'dwqa-answer') {
$term = term_exists('ar', 'language');
if ($term !== 0 && $term !== null`enter code here`) {
wp_set_post_terms( $post_id, array ('ar'), 'language', true );
}
}
}
add_action( 'save_post', 'update_post_language' );

Update user meta after woocommerce checkout form process

I am using woocommerce with Wordpress and have added some custom fields to the checkout:
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');
function my_custom_checkout_field( $checkout ) {
$extra_fields = array('job_title', 'company', 'telephone', 'occupation');
foreach($extra_fields as $key => $value) {
woocommerce_form_field($value, array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __($label),
'value' => '',
), $checkout->get_value( $value ));
}
}
Now currently, these appear in the checkout fine, not sure if using woocommerce_after_order_notes is right in this case. I have also added some custom fields to the user meta that correspond to the fields added to the checkout - which all display in the user profile page:
function add_contact_methods( $contactmethods ) {
$contactmethods['job_title'] = 'Job Title';
$contactmethods['company'] = 'Company Name';
$contactmethods['telephone'] = 'Telephone';
$contactmethods['occupation'] = 'Occupation';
$contactmethods['refer'] = 'How you heard about us?';
return $contactmethods;
}
add_filter('user_contactmethods','add_contact_methods',10,1);
As you can imagine, if I update any of these field in any profile page, it works fine but what I cant seem to do is update the user meta when a new user makes a purchase, it does not update the user meta for these fields in the database.
I understand alot of how this works, and understand that I must hook into a Woocommerce process to add the fields into the process. So I have added this code into my functions too:
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
function my_custom_checkout_field_update_user_meta( $user_id ) {
global $extra_fields;
foreach($extra_fields as $key => $value) {
if ($_POST[$value]) update_user_meta( $user_id, $value, esc_attr($_POST[$value]));
}
}
Now the twist is, this works if a user who is already signed in as a member, makes a repurchase and goes through the checkout - the reason this works is because $user_id already exists, but when a new user is checking out, they do not yet exist as a user, hence the function cannot update the user meta of NIL where $user_id does not exist.
My question is, how do I hook into the checkout process, presumably AFTER the user has been created, so I that I can get the $user_id returned, and execute this function to update the user meta.
class-wc-checkout.php line 639 creates the new user with $this->customer_id = wp_insert_user( apply_filters( 'woocommerce_new_customer_data', $new_customer_data ) ); The new customer data is an array listed just above that line.
Following that, you can access the user id with line 649's action do_action( 'woocommerce_created_customer', $this->customer_id );
It is unlikey, in your case, you will need to use the filter, but simply add the action 'woocommerce_created_customer', pull in the id, and add the meta.
When customer is not logged in checkout page should be acceptable field customer want to create a new account.Below sample code change in checkout page when customer order a new item and update user meta data.
function user_extra_meta_fields(){
return array(
'job_title' => __( 'Job Title', 'yourtext_domain'),
'company' => __( 'Company Name', 'yourtext_domain'),
'telephone' => __( 'Telephone', 'yourtext_domain'),
'occupation' => __( 'Occupation', 'yourtext_domain'),
'refer' => __( 'How you heard about us?', 'yourtext_domain'),
);
}
function add_contact_methods( $contactmethods ) {
$contactmethods = array_merge( $contactmethods, user_extra_meta_fields());
return $contactmethods;
}
add_filter('user_contactmethods','add_contact_methods',10,1);
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');
function my_custom_checkout_field( $checkout ) {
foreach( user_extra_meta_fields() as $name => $label) {
$value = '';
if( is_user_logged_in() )
$value = get_user_meta( get_current_user_id(), $name, true );
woocommerce_form_field( $name, array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => $label,
), $value );
}
}
add_action( 'woocommerce_checkout_process', 'user_fields_woocommerce_checkout_process' );
function user_fields_woocommerce_checkout_process(){
if( is_user_logged_in() )
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta' );
else
add_action( 'woocommerce_created_customer', 'my_custom_checkout_field_update_user_meta' );
}
function my_custom_checkout_field_update_user_meta( $user_id ) {
foreach( array_keys( user_extra_meta_fields() ) as $meta_name ){
if( isset( $_POST[$meta_name] ) ){
$meta_value = $_POST[$meta_name] ? esc_attr($_POST[$meta_name]) : '';
update_user_meta( $user_id, $meta_name, $meta_value );
}
}
}
// if want to validate field
add_action( 'woocommerce_after_checkout_validation', 'user_fields_woocommerce_after_checkout_validation' );
function user_fields_woocommerce_after_checkout_validation( $posted ){
$validate = true;
if( ! is_user_logged_in() && empty( $posted['createaccount'] ) )
$validate = false;
if( $validate == false )
return;
$meta_data = user_extra_meta_fields();
foreach( array_keys( $meta_data ) as $meta_name ){
if( empty($_POST[$meta_name]) )
wc_add_notice( sprintf( __(' <strong>%s</strong> is required.', 'yourtext_domain'), $meta_data[$meta_name] ), 'error' );
}
}

Resources