Wordpress Hide child posts of custom post type in wordpress admin - wordpress

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');

Related

Can I make a page appear as a blog post in wordpress?

I created this page on my site: http://christmaspast.media/christmas-podcast-guide/ Each of the items on the page is a post that I created with a custom post type.
My question is whether I can treat this entire page as a blog post, and have it appear on my blog homepage along with all the other posts in my blog feed.
If not, is there a way to accomplish the same thing with a post? (AFAIK, it's not possible to embed multiple custom posts into another post, which is why I created a page instead.)
You need to use a filter pre_get_posts to modify the main query. Check Docs.
Replace custom_post_type string with yours.
function pref_add_custom_post_type_to_loop( $query ) {
// don't run in admin area
if ( ! is_admin() ) {
// add custom post types
if ( $query->is_feed() || ($query->is_main_query() && $query->is_front_page()) ) {
$query->set('post_type', array('post', 'custom_post_type'));
}
}
}
add_action('pre_get_posts', 'pref_add_custom_post_type_to_loop');

Show only user-created taxonomy entries

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');

Only logged in users should be able to view product in WooCommerce

I want to restrict WooCommerce products, shop page and category pages to logged in users only.
I do not want to achieve it with any plugin.
Please let me know if anybody done it before with any hook/filter/action.
Or I have to make WooCommerce template pages and add condition over there.
If I was to do this I would hook into the init action that WordPress offers.
Then do something like this in your theme's functions.php file:
function woo_check_logged_in()
{
if ( (is_product() || is_shop() ) && is_user_logged_in() )
{
}
else
{
die("You must be logged in to view this page");
}
}
add_action('init', 'woo_check_logged_in');
I haven't tested this but I believe it should get you on the right path without having to use any plugins.

Change menu order on backend of wordpress custom post type

I'm in the backend of wordpress, I have a custom post type called 'work' and I want to re-order the work projects (in the backend) by menu order. I've found an action hook called 'pre_get_posts' which returns the query, but the query has the post_type listed as 'page'. How do I hook into the query that prints the custom post types in the back end?
This is what I'm trying to accomplish but don't know the right hook or filter:
function search_filter($query) {
if($_GET['post_type'] == 'work' && is_admin())
$query->query_vars['orderby'] = 'menu_order';
return $query;
}
add_action('pre_get_posts', 'search_filter');

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