Wordpress search doesn't show custom post types and fields - wordpress

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/

Related

Wordpress REST api, how to seach custom field values

I'm using the Wordpress REST api to get all my post data, and using a frontend framework to display them. I've created several post types, who each have different custom fields (I created the fields using the Advanced Custom Fields plugin and the acf-to-rest-api plugin to add the fields to the json output).
Now I'm trying to use Wordpress' search function to search through custom field values, using a url like this:
/wp-json/wp/v2/articles?search=test
Currently, only the body text and title values are being used in the search, not the custom fields.
I've found a plugin to allow the search to include custom field values ( https://wordpress.org/plugins/custom-fields-search/ ), but when I installed it, nothing changed. I don't think the plugin works with the rest API.
Is there another way to achieve this?
Really late reply, but for people stumbling upon this post I have an answer to this problem:
add_action(
'rest_api_init',
function () {
register_rest_field(
'search-result',
'excerpt',
array(
'get_callback' => function ( $post_arr ) {
return $post_arr['excerpt'];
},
)
);
}
);
In the callback you can pretty much return anything you want, does not have to be specifically taken from the $post_arrvariable.

How to change the complete wordpress structure to show most recent posts according to last modified

In wordpress blog, posts appears according to most recent (new). Here i want to change it to show all recent posts according to last modified. Does it possible?
I don't know where the code is, checked theme functions but not found anything.
Can anyone help me?
You need to use this hook to get the post order by last modified.
function custom_loop( $query ) {
$query->set( 'orderby', 'modified' );
}
add_action( 'pre_get_posts', 'custom_loop' );
Use this in functions.php

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

Create a blog post from content in another post type

The Wordpress site I'm working on has a section for "News" (which is the regular blog/posts) which will be used for any news the company has to write about. Then I have a custom post type for Promotions, which has it's own page.
I want the client to be able to add his promotion content through the custom post type, which is going on the Promotions page, however I'd like this content to also be "cross posted" into the blog/news without forcing the client to write it up twice.
Is there a way to do this? Thanks.
Just a note: The reason I have the promotions as a custom type on it's own instead of just having them do it all from the blog is because I needed custom fields that would be unnecessary for any other kind of blog post.
Two options:
1) Use the Shortcode API
And in your cross-post you'd add the shortcode [crosspost id="POST-ID"]. Where POST-ID corresponds to the numeric ID of the other post (post type). Instead of ID, the title could be used, see the function get_page_by_title.
Create your own plugin for this. Add a sample shortcode from the Codex and use the function get_post to get the contents of the cross-post.
2) Use Advanced Custom Fields plugin
With it, adding meta boxes with custom fields is a breeze. And it has a Post Object field that's basically a Cross Post functionality.
You could do it a lot more simply by adding a filter to wp_insert_data(). For example, in your theme's functions.php file add the following:
add_filter('wp_insert_post_data', 'post_to_other', 99, 2);
That filter will then run anytime you add a new post. In the function post_to_other(), you look to see what type of post is being submitted. If it's a promotion, then insert a second copy as a News item.
function post_to_other($post_id, $post){
/** check $post to see what type it is, if it's a promotion */
if($post->post_type == 'promotion'){
$second_post = array(
'post_type'=> 'post',
'post_title'=> $post->post_title,
'post_name' =>$post->post_name,
'post_content'=> $post->post_content,
'post_author'=> $post->post_author,
'post_status'=> 'publish',
'tax_input'=> array('taxonomy_name'=>array('news'))
);
wp_insert_post($second_post);
}
}
I'm running out the door so I don't have time to double check the exact code but that's the basic structure of it. The tax_input bit is optional, lets you specify a category if you want. You'll probably need to tweak it a bit but that's the basics.

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.

Resources