Combined Search for Author & Custom Post Type - wordpress

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;
}

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

Wordpress search doesn't show custom post types and fields

I have done some research on this topic and followed many tutorials but nothing seems to work, I was wondering if someone could help me out? I want to allow the search form in my Wordpress site to also include custom post types and custom meta fields. I would really appreciate it if someone could help me out. THANKS!
Archives.php only shows content of type 'post', but you can alter it to include custom post types. Add this filter to your functions.php file:
function namespace_add_custom_types( $query ) {
if ( $query->is_search )
$query->set( 'post_type', array( 'post', 'YOUR_CUSTOM_POST_HERE') );
return $query;
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );
Wordpress search functionality looking search query only in 'posts' table in DB, but custom fields are saved on 'post_meta' table. So, firstly you need to LEFT JOIN these two tables, secondly change query to DB, and finally - prevent duplicates in searching. Please look at this link, here is the code you must paste into functions.php with an explanation -> https://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/

Best method for creating a custom Wordpress post type just like a page

I have a site where I need to add a post type that has all of the exact same features as a normal PAGE in Wordpress but is a post type of "AGENT".
I thought the easiest way to do this would have been to create a normal page with a specific category that I could reference elsewhere in the code...but I know categories are not available on pages.
Is the best way to do this with a custom post type, or is there an easier method?
Thanks!
Well, we can add categories to pages.
add_action( 'init', 'wpse34528_add_page_cats' );
function wpse34528_add_page_cats()
{
register_taxonomy_for_object_type( 'category', 'page' );
}
Or you can create a Custom Taxonomy and assign it to Pages.
Or create the Custom Post Type and configure it as you wish.

Wordpress - remove some posts from query

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.

Wordpress: Different look for different posts

I've looked everywhere to work out how to do this, but I'm having no luck. I read up on Post Formats, but I don't think that's what I'm looking for.
All the posts on the frontpage are the same, but say, for features or reviews you can have an entirely different look compared to the "standard" single.php post.
Examples:
Homepage //
http:// ausdroid.net (All posts look the same)
Standard single post //
http://ausdroid.net/2013/02/03/samsung-galaxy-nexus-from-vodafone-receives-android-4-1-2-update
Different single post (for a review in this case) //
http://ausdroid.net/2013/02/02/huawei-ascend-d1-quad-review
Thanks!
Sample on using category to present different Singles:
single.php
<?php
if ( in_category( $foo ) )
require( 'single-foo.php' );
else
require( 'single-default.php' );
So you'll have the above simple code in the usual single.php, put your previous single.php to single-default.php, and create your "other" Single layout in single-foo.php.
$foo can be category ID or Slug.
in_category()
EDIT
You can modify this to work with Post Formats:
In functions.php:
add_theme_support( 'post-formats', array( 'aside', 'gallery' ) );
... then, change the if statement in my sample above to use has_post_format() instead.
has_post_format().
http://www.wpbeginner.com/wp-themes/create-custom-single-post-templates-for-specific-posts-or-sections-in-wordpress/
this explains it pretty well using " Single Post Template" plugin, if thats an option
You can try adding custom fields to the posts and check them in the single.php to show different layouts. See this video for and example of a plugin doing just that - youtube.com/watch?v=L3VXnryN9iY

Resources