Wordpress - remove some posts from query - wordpress

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.

Related

Show custom post archive when custom post not specified

I have a custom post type called produce set up in WordPress and a custom taxonomy called produce_category.
The URL format for posts in this custom post type is in this format http://mywebsite/produce/%produce_category%/%postname%/. Of course %produce_category% and %postname% get substituted accordingly, a working example url would be http://mywebsite/produce/fruits-and-vegetables/avocado.
What I would like to do is show the produce_category custom taxonomy archive if a user visits http://mywebsite/produce/%produce_category%, without specifying post name at the end, e.g http://mywebsite/produce/fruits-and-vegetables to show all produce in fruits-and-vegetables produce_category.
Besides that when a user visits http://mywebsite/produce/ I would like to show all the produce archive. EDIT: I have this working now.
I know how to create the archive pages totally fine and have no problem with that. I am stuck at creating permalinks. When I visit http://mywebsite/produce/%produce_category% I get a 404 error.
I'm looking for advise on the best way to implement this. Currently I am using Custom Post Type Permalinks and CPTUI.
The CPTUI custom taxonomy settings interface does not allow me to have a blank in the custom rewrite slug. It defaults to the custom taxonomy slug, produce_category, when I don't fill in anything.
This gives the front-side produce_category taxonomy archive url as http://mywebsite/produce/produce_category/%produce_category%/ e.g. http://mywebsite/produce/produce_category/fish-and-seafood/ when what I want for the archive is http://mywebsite/produce/fish-and-seafood/.
Please help with suggestion on the best way I can achieve the custom taxonomy url.
Thank you all.
Try this code. It will help you to achieve your url structure... Make sure you update permalinks after saving it to functions.php
function custom_produce_category_link( $link, $term, $taxonomy )
{
if ( $taxonomy !== 'produce_category' )
return $link;
return str_replace( 'produce_category/', '', $link );
}
add_filter( 'term_link', 'custom_produce_category_link', 10, 3 );

wp_insert_post not saving custom taxonomy on devices

I've faced a weird problem on my wordpress website.
I am using the wp_insert_post on front end to save a custom post type and its custom taxonomy. On PC it saves perfectly the custom taxonomy, but when I try to submit on mobile or ipad, then its not saving it.
here's my code:
$post = array(
'post_title'=>$_POST['message_title'],
'post_status'=>'pending',
'post_type'=>'mondd_el',
'tax_input'=>array('me_kategoria'=>$cat_id),
'post_content'=>$_POST['my_message']
);
$postid = wp_insert_post($post);
Thanks
ARE YOU SURE THE CURRENT USER HAVE CAPABILITY ON WORKING WITH TAXONOMY ? PLEASE CHECK THAT... THIS IS WHAT YOU GET FROM WORDPRESS DOCUMENTATION
"tax_input: Equivalent to calling wp_set_post_terms() for each custom taxonomy in the array. If the current user doesn't have the capability to work with a taxonomy, then you must use wp_set_object_terms() instead."

WordPress hiding categories view from edit posts

I have created a edit link on my wordpress where subscribers can edit their posts. When they click this it takes them back to the admin portal to edit post. I want to make sure they can't see the categories widget on the right side how do I remove that from a user seeing this?
<?php edit_post_link(__("Edit Post"), ''); ?>
I ahve this un the function.php file but need to know how to make it just for subscribers.
function wpse60590_remove_metaboxes() { if() remove_meta_box( 'categorydiv' , 'post' , 'normal' ); remove_meta_box( 'tagsdiv-post_tag' , 'post' , 'normal' ); } add_action( 'admin_menu' , 'wpse60590_remove_metaboxes' );
You need to modify the capabilities of these users' role to prevent them from working with categories. The capability you need to disable for their role should be "manage_categories".
Just make sure all of the users you wish to limit are in the same role (e.g. "Contributor" or something).
In my experience the easiest way to manage capabilities for roles is the Members Plugin.
Once installed, go to Users -> Roles -> Select the role you wish to change. Find "manage_categories", uncheck it, and save.
If you are using a custom post type, we may have to add some settings where you register the taxonomy to specify the ability to assign a category to a post.

Combined Search for Author & Custom Post Type

I have searched questions similar to mine but with no luck finding the answer I need.
I have Authors and I have Custom Post Types (CPT). My search results already display all CPT's -- but, additionally, I need something more specific than that. I need my search function to allow combined queries for a specific Author and specific CPT. For example, all Blogs by Albert Einstein.
This url "/?s=%20&author_name=alberteinstein" returns all posts across CPT's by Albert Einstein.
But if I add "&post_type=blogs" for the full url to filter for the CPT like this:
"/?s=%20&author_name=alberteinstein&post_type=blogs"
it does not filter for just Blogs -- it still returns all CPT's by the Author, same as above.
I need to be able to query for an Author and specific CPT.
This has been driving me crazy for weeks. Any help would be greatly appreciated.
This may help (as worded on the WordPress Codex post types page). Basically, it may be that your custom post type (CPT) isn't registered for archive queries although it is legitimately registered for use as a CPT.
Registering a custom post type does not mean it gets added to the main query automatically. If you want your custom post type posts to show up on standard archives or include them on your home page mixed up with other post types, use the pre_get_posts action hook.
// Show posts of 'post', 'page' and 'movie' post types on home page
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'page', 'movie' ) );
return $query;
}

How to map an arbitrary URL to a function in a Wordpress plugin

I'm trying to create a Wordpress plugin that redirects visitors of example.com/redirect/XXX to a different page based on the value of XXX. I think I know how to do the redirect logic, but I don't know how to make sure that my Wordpress plugin function will be called when a visitor goes to example.com/redirect. Right now I just get a 404. There are other solutions that involved changing the .htaccess file, but I want this to function as a standalone plugin. Thanks!
When I need this kind of things i just create a common page with template as the "plugin", a page with all the functions that I need.
For example, if i need a shopping cart, I just create a cart.php as:
<?php
/*
Template Name: Cart
*/
// functions here
?>
And I go to my wp-admin and create a page with Cart as its template.
Depending on what exactly you want to do which is quite vague ( when you say page you actually mean page ? post ? cpt ? and when you say plugin functions - what are they ? and do you use permalinks ?)
.. but under some conditions you could use wp conditionals .
example ( from codex )
is_singular( 'foo' )
// Returns true if the post_type is "foo". execute plugin hook
is_singular( array( 'foo', 'bar', 'baz' ) )
// Returns true if the post_type is "foo", "bar", or "baz".
// See also the Custom Post Types book.
or if you aim at filtering you can always hook to pre_get_post with is_main_query() or any other conditional //

Resources