Wordpress search custom fields - wordpress

I have a custom field with meta_name is product_id. I am trying to determine how to search that field when using the wordpress search.
I know the wordpress search results page uses query_string which contains the "s" value for the keyword searched I just don't know how to change my code below to search the custom field mentioned.
$loop = new WP_Query("$query_string . '&posts_per_page=-1&post_type=product'"); ?>

think this section in the codex should have the answers you need with regards to adding in custom field parameters to you query:
http://codex.wordpress.org/Function_Reference/WP_Query#Custom_Field_Parameters
Not sure whether that will allow you to search within a particular custom field though. Can't be certain but I think you'd have to make some changes on the search form end of things. See how you get on with adding in the custom field parameters.
Off the top of my head I would think you would need something like:
$query = new WP_Query( array(
'meta_key' => 'product_id',
'meta_value' => $query_string,
'posts_per_page' => '-1',
'post_type' => 'product'
) );
Not tested though!

Related

ACF - Gallery Field -> reverse lookup to find all posts where that image is in the Gallery Field

I am trying to do a reverse query to show me all of the posts that an image may be attached to using the ID of that image.
The kicker is, the connection would be done using the ACF Gallery Field
https://www.advancedcustomfields.com/resources/gallery/
I have the ID's of the images noticed there was a meta data field for ["uploaded_to"] that I thought originally would work, but this is just a single ID, not an array, and is also only the post that the image was uploaded to and won't show if the image was uploaded directly to the media library.
The only thing I can think of which would be very resource heavy and slow would be to loop through all posts and each field of those posts for the Gallery Field and then match on the ID of the image, and when there is match, output the ID of the post to an array. This sounds just messy and bloated.
Is there an easier way to do this type of query?
It is possible to craft a WP query where you search based on meta fields.
$wp_query = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => -1, //query every post
'meta_query' => array(
array(
'key' => 'uploaded_to',
'value' => $value_your_looking_for,
'compare' => 'LIKE'
)
)
));
I don't know what for values the field uploaded_to returns, but I'm guessing that it is an array with images id's

hide page wordpress except if you have direct link(how to hide it from indexing, etc)

I have a part of site(one page) that I want to hide and you can access it only if you have a direct link(you don't need to be registered user(public)). So what I need to do to hide it from indexing and stuff like that(I know that is the only way to protect it if page has status public.
Considering you are using loops everywhere for showing posts.
Add 'post__not_in' => array($post_ID) wherever you are using the loop in the query.
$my_query = new WP_Query(
array(
......,
......,
'post__not_in' => array($post_ID)
));
This would remove that post from the loop,hence will not show it.

Change wordpress to display search results in chronological order

This is a Thesis theme site using the core WP search functionality. Not looking for a new plugin solution, just how to alter the search results to change order of posts from relevance to chronological order. Any help is greatly appreciated.
Some of native wp search results, like terms do not have any type time info stored. You can use WP_User_Query with order of records. It will be not exactly what you expected but will have chronological order:
$args = array(
'search' => 'search_patern_from_form',
'orderby' => 'id',
'order' => 'DESC'
);
$user_query = new WP_User_Query( $args );
See order by section of WP_User_Query codex page

Adding postmeta default values after adding post

I am using Wordpress XMLRPC to add posts to my blog.
However, after running this.
$data = array(
'title' => $title,
'description' => $content,
'post_type' => 'post',
'categories' => array($category),
'post_status' => 'publish'
);
$addedPostReturn = $this->_client->query('metaWeblog.newPost',
array(0,$username,$password,$data,1));
This adds the post fine but doesn't add the postmeta information.
If i open the post, click Update, all the default postmeta gets updated. However, I would like to add all the default postmeta information with my php script instead of manually (or else it kinda defeats the purpose).
Is there anyway, either with xmlrpc or regular wordpress functions to create the postmeta custom fields by using their default values? If not, is there a way to have the list of all the custom fields i need to manually add using the custom fields section of the metaWeblog.newPost function? (I don't want to add some but not others. I rather do a complete job)
Thanks in advance... any help is appreciated! :)
EDIT:
$postUpdateContent = array();
$postUpdateContent['ID'] = $newPostId;
$postUpdateContent['post_content'] = $sameContent;
wp_update_post( $postUpdateContent );
didn't seem to do the trick...

Wordpress WP_Query call posts AND pages

I have a feature slider set up that draws in posts that are tagged 'feature'
$my_query = new WP_Query(array(
'showposts' => 3,
'tag' => 'feature' ));
Is is possible to draw in posts AND pages?
I know you can draw pages with 'post_type'=>'page' but can you mix the two?
You can specify an array value for the post_type parameter, as such:
$my_query = new WP_Query(array(
'post_type' => array('post', 'page'),
'tag' => 'feature'
));
See this page for more info: WP Codex
#fivedigit Thanks but I went with this in the end:
$my_query = new WP_Query(array(
'post_type' => array('any'),
'tag' => 'feature'
));
Although your version may come in handy in the future!
For anyone having to edit older code that doesn't use an array passed into WP_Query, you can add &post_type=any to get posts and pages (and other content). Unfortunately I don't see a way to get posts and pages (without other types) without using an array, since post_type would then require an array as the examples above show. However, this should be good enough if you are searching for a particular category anyway.
Example (this from vSlider v4.1.2 where &post_type=any is added so that pages are included in the slider):
$recent = new WP_Query($randimg."cat=".$options['imgCat']."&showposts=".$options['slideNr']."&post_type=any");
Thanks to #fivedigit and #my-jonny-wood for the answers above that led me to figuring this out and fixing the slider on my site!

Resources