Show only user-created taxonomy entries - wordpress

iam implementing an event system on wordpress which allows an restricted user the following:
Create a new organizer (custom-post-type)
Create an event (custom-post-type)
Creating an event the user should choose an organizer for a special event by a list. Therefore i handle the organizer cpt as a taxonomy inside the event cpt.
Now my question is:
How can i only show the organizers, which this specific user has created? I face the problem, that all existing events are shown on every event for every user.
If you need any code or screenshots let me know, thank you very much in advance!

Solved it with Advanced Custom Fields plugin (Relationship Field). It is possible to relate cpts into another cpt. I learned to NOT use a cpt as a taxonomy.
Approach for filtering only user created posts

You can achieve it by adding the following code to your functions.php file
function posts_for_current_author($query) {
global $pagenow;
if( 'edit.php' != $pagenow || !$query->is_admin )
return $query;
if( !current_user_can( 'edit_others_posts' ) ) {
global $user_ID;
$query->set('author', $user_ID );
}
return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');

Related

Wordpress Hide child posts of custom post type in wordpress admin

I created a custom post type called "Companies".
Every time a post of post type companies is created, a child post is also created.
I would like to hide the child posts of this companies post type in the backend.
Is there a way to do this?
Thanks!
I managed to do this by using the following code:
function cleanup_companies_posttype_list($query) {
global $pagenow;
if (isset($_GET['post_type']) && $_GET['post_type'] == 'companies' && $pagenow == 'edit.php') {
$query->set('post_parent',0);
}
}
add_filter('pre_get_posts', 'cleanup_companies_posttype_list');

Woocommerce woocommerce_order_status_processing trigger only in user side

Hi I would like to ask you guys if this is possible in Woocommerce hooks
the hook I'm using is this one,
woocommerce_order_status_processing
I want this hook only to be called in user side after payment, which is works ok but in the admin if I change the oder status, this hook is also triggering, can I disabled my custom hook in admin and will run/trigger only for the user side?
add_action( 'woocommerce_order_status_processing', 'order_extracode' );
function order_extracod( $order_id) {
.....
}
the above code is the function and hook I added, I tried !is_admin() but it is not working, and still processing this function in Admin Orders
thanks (TIA)
This hook will be executed every time there's a statu change.
What you can do is to decide if you want to execute the code in the front or in the Dashboard.
add_action( 'woocommerce_order_status_processing', 'order_extracode' );
function order_extracod( $order_id) {
if( ! is_admin()){
// Your code here
}
}
The only conditional tag to detect if you are in the dashboard or not is the is_admin()
WooCommere cannot tell the difference between who is triggering the woocommerce_order_status_processing action.
If you want something to happen when the user completes payment, you could try the woocommerce_payment_complete hook in abstract-wc-order.php.
Alternatively you can use current_user_can() function to determine the whether the hook can be executed or not, like this
if( !current_user_can( 'administrator' ) && !current_user_can( 'manage_options' ) ) {
//do your stuff
}

How do you show media posted by a certain user ? Wordpress

How do you show media posted by a certain user in Wordpress?
Say I have the loop...
and im using get_the_author_meta() to retrieve user inf
Now I want to show all the images a certain user has uploaded.
Have no idea where to begin.
Just want to show a list of images that one user has uploaded.
A very common code to allow users to see ONLY their own attachments in the upload page is :
add_action('pre_get_posts','users_attachments');
function users_attachments( $wp_query_obj ) {
global $current_user, $pagenow;
if( !is_a( $current_user, 'WP_User') )
return;
if( 'upload.php' != $pagenow )
return;
if( !current_user_can('delete_pages') )
$wp_query_obj->set('author', $current_user->id );
return;
}
So based on this, and removing the conditionals you do not need - you could try :
add_action('pre_get_posts','users_attachments');
function users_attachments( $wp_query_obj ) {
$wp_query_obj->set('author', '30' ); // ID of user , or any other meta..
return;
}
or Filter the Query for example :
function user_files_only( $wp_query ) {
global $current_user;
$wp_query->set( 'author', '30' ); // ID of user , or any other meta..
}
add_filter('parse_query', 'user_files_only' );
Both examples actually alter the main query in some way , so you might want to remove the action after applying it . Not sure how this will work on the loop.
You could use the pre_get_posts Filter and to filter the query..
Give a tag as user x for a certain user who posts his media in different categories, now it is easy if anyone press that tag then all the media uploaded by a particular user gets displayed one by one.(i mean that you must use tags for all the posts of the user-x by giving the same tag for all his uploaded media )

Getting hold of metadata when creating a post in WordPress

I am using the save_post action to inspect a metadata field in a custom post and take some action on that value. This is the essential guts of how I am doing it:
add_action('save_post', 'my_save_post');
function my_save_post($post_id)
{
// Check if not autosaving, processing correct post type etc.
// ...
// Get the custom field value.
$my_field_value = get_post_meta($post_id, 'my_field', true);
// Do some action
// ...
}
This works fine when updating the post through the admin page. However, when first creating the post, the my_field_value is always empty. The field does get saved correctly, but this action trigger does not seem to be able to see it, nor any other custom field values.
I would like the action to be performed on all posts of this type created, and I will be importing many through the CSV Imported plugin. Even then, the custom fields do get imported correctly, and the action trigger does get fired for each row imported, but the save_post action still cannot see the custom field value.
So far as I can see from documentation, the post has already been created by the time this action fires, so I should always be able to see that custom metafield.
The answer, it seems, is in the order in which things happen. When creating a post from a form, the custom fields are all collected by the appropriate actions and added to the post before my save_post action fires. This means my trigger is able to see those custom field values.
When importing from CSV, the basic post is created first, and then the custom metafields are added. The save_post trigger fires on the first creation, before the metafields are added, and so the custom field data is not visible to the save_post action.
My solution was to catch the updates of the metadata using the updated_post_meta and added_post_meta actions as well as the save_post action:
add_action('updated_post_meta', 'my_updated_post_meta', 10, 4);
add_action('added_post_meta', 'my_updated_post_meta', 10, 4);
function my_updated_post_meta($meta_id, $post_id, $meta_key, $meta_value)
{
// Make sure we are handling just the meta field we are interested in.
if ($meta_key != 'my_custom_field') return;
if (wp_is_post_revision($post_id)) return;
if (get_post_type($post_id) != 'my_post_type') return;
if (trim($meta_value) == '') return;
// Do my custom task (linking this post to a parent post in a different
// post type). This is the same task performed by the save_post action.
my_link_product_track($post_id, trim($meta_value));
}
That is essentially what I do, and it seems to work well. I do encapsulate all the above into a custom class in the theme, and don't recommend using global scope variables as shown here, but this is just to show the method.
You should look at using $post->ID instead of $post_id -
$my_field_value = get_post_meta($post->ID, 'my_field', true);
get_post_meta in the Codex
EDIT:
Could you do something like this?
if($post->ID == ''){
$pid = $post_id;
} else {
$pid = $post->ID;
}
//$pid = $post->ID or $post_id, whichever contains a value
$my_field_value = get_post_meta($pid, 'my_field', true);
something that looks for a value in $post->ID and $post_id, and uses whichever one isn't blank?

Populate Custom Category Taxonomy with Data in WP Plugin

I'm writing a wordpress plugin that allows people in the admin to hide/show content specific to US states. I have a custom category taxonomy called States, and it lists all of the states. Admins can check which states they want the post to appear in. Pages and posts will not show up in the loop if the user's state doesn't match up with the posts' selected states.
Now, my question is, how do I populate the plugin with all of the states' data upon install (or remove it upon uninstall)?
This should work. You'll need to add the rest of the states, and make sure that your taxonomy is actually called "States", but other than that it should be fine:
<?php
$foo_states = array(
'Alabama',
'Alaska',
'Arizona',
'Arkansas'
);
function foo_install() {
global $foo_states;
foreach ( (array) $foo_states as $state ) {
wp_insert_term($state, 'States');
}
}
register_activation_hook(__FILE__, 'foo_install')
function foo_uninstall() {
global $foo_states;
foreach ( (array) $foo_states as $state ) {
wp_delete_term(get_term_by('name', $state, 'States')->term_id, 'States');
}
}
register_deactivation_hook(__FILE__, 'foo_uninstall');
?>

Resources