Wordpress REST api, how to seach custom field values - wordpress

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.

Related

Manipulate woocommerce product edit page

I am new to wordpress and woocommerce development and I am just wondering, how to manipulate an admin-screen in a clean, updateable way.
For example, I want to add a custom field to a product edit page (see screen):
I know, that I have to write a custom extension, but is it possible, to manipulate admin-screens of other extensions? I couldn't find any suitable tutorial? Maybe someone has a hint, where to start?
The feature of creating custom fields for products is baked right into WooCommerce, whether to implement it directly in functions.php or do the same via a plugin is left to one's sole discretion.
Remi Corson has written an excellent article detailing the same.
Here's the gist :
1.Create the fields using the woocommerce_product_options_general_product_data hook
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
// Define your fields here.
// You can create text, textarea, select, checkbox and custom fields
}
2.When product is saved save the value entered in your custom field using woocommerce_process_product_meta hook
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $_post_id ) {
//save field values
}
WooCommerce is a WordPress plugin that will help you to turn your website into an eCommerce store.
Yes, you can write an Extension ( or ADD-On) for this plugin, in fact there are already hundreds of Extension ( free and Paid ) have been made for it.
In order to create an Extension ( or ADD-ON ) for this plugin, you need to understand 2 things:
WooCommerce API
http://docs.woothemes.com/document/create-a-plugin/
WordPress API https://codex.wordpress.org/Writing_a_Plugin

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/

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.

what hook to hook into for creating custom post types?

I hate putting things into the functions.php file and therefore wanted to hook into a pre-existing WP-hook to register/configure my custom post-types (and taxonomies). Somewhere I was reading that "template-redirect" would be a good one but it appear that that hook isn't fired when you're on the admin pages and therefore is fairly useless.
Any help would be appreciated.
You can use init hook.
An example of registering a post type called "book".
function codex_custom_init() {
$args = array( 'public' => true, 'label' => 'Books' );
register_post_type( 'book', $args );
}
add_action( 'init', 'codex_custom_init' );
Reference: Codex.
I was reviewing my open questions and it reminded me that I hadn't closed this one out. Marty's answer was helpful but really pointed to a different solution path. In retrospect, I'm not sure what hook I had tried but the kind of obvious one is "init" and I'm using that now and it works.
Here's my flow:
My plugin is loaded with the 'plugins_loaded' event
It does some basic initialisation and then hooks into the 'admin' hook
When the 'admin' event is fired my plugin then this fires off the following function:
function add_hooks () {
// fire a hook that a configuration file can pick up
do_action ( 'lg_custom_types_definition');
// now fire hooks to register custom types
do_action ( 'lg_custom_type_cpt_registration' ); // register
do_action ( 'lg_custom_types_registered_post_types');
do_action ( 'lg_custom_type_tax_registration' ); // register
do_action ( 'lg_custom_types_registered_taxonomies');
}
This approach gives me a completely decoupled approach which means I can enable the "custom_types" plugin and I now have the 'capability' installed. Then I create a configuration plugin that hooks into the events that the capability has added.
Hope this helps.
You could use an include file within functions.php to include all your custom work.
<?php
// functions.php
include('inc/custom-functions.php');
?>
I've created a very simple page to create your custom post types,
you input the options you want for the custom field and it spits out the code needed in order to generate it in wordpress..
its located here: http://martin-gardner.co.uk/wordpress-custom-post-type-generator/
for example:
Input the Post Type Name: Video select where in the menu you want it positioned.
then select the options you want for that custom post type.
Edit labels, if you want, by default just uses name,
Read more on custom post types and the register_post_type #
http://codex.wordpress.org/Function_Reference/register_post_type
hope that helps a little ;)
Marty

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