How to query wordpress posts by custom field - wordpress

I am attempting to have lots of posts in a specific category. The post will have a custom field that will be a date. I need the posts to be organized by the category, the custom field, and each page of posts should be separated by month of the custom field. So hard to explain...
Page of posts for the current month. Links at the bottom for Previous and Next month. Sorted by custom field, not post date. And it should only be in the specific category.
Any ideas on what I can do to achieve this?

There are several things you can do. If you want to sort it by date, you could just modify the published date in the right sidebar of the Add Post screen.
If you want posts to be only from a certain category you can create a custom query like this
query_posts('category_name=My Category');
And then order by your custom field using: orderby=meta_value
So the full query is:
query_posts('category_name=My Category&orderby=meta_value&meta_key=dateField')
Hopefully that helps you get on the right track.
Documentation:
http://codex.wordpress.org/Function_Reference/get_post_meta
http://codex.wordpress.org/Function_Reference/query_posts

Related

Wordpress Woocommerce: Add custom condition in product query based on custom filter

I am working on a woocommerce project with little bit knowledge on wordpress. Now, I have got a requirement which is shared below:
1. Show date calendar on product listing page top of list not in sidebar. I am saving product availiblity based on date ranges in separate table. This table has product id, date start and date end.
2. Show sizes dropdown on product listing on top of list. Sizes filter is already showing in sidebard but client wants it on top. So, I wil disable it in sidebar.
3. When customer will select date and size, product list should be filter accordingly.
I have read wordpress documentation where wc_query has option to use either "EXISTS or NOT Exists" through ws_meta_query. But I am not able to understand, how can I put filter on product query using either "EXISTS or NOT EXISTS" with my custom raw query?
Can someone please guide me how can I implement whole requirement along with how can I EXISTS using raw sql query with product query?
Thanks in Advance
For first 2 points, you'll need woocommerce_before_shop_loop action to display your custom filters.
and for 3rd point, you'll need pre_get_posts action to control your queries.

How to access an ACF field on a category from within pre_get_posts in functions.php to modify archive query

Long title, but what I'm trying to accomplish is
1) Adding a Post Object field to the Add/Edit Category page to specify a single post to use as a featured/sticky post for that category. As far as I can tell there's no way to do this (featured category post). Have used https://wordpress.org/plugins/category-sticky-post/ in the past but seems dead and starting to act buggy.
2) Display that post on top of the respective archive page 1, and then start with the normal loop for that category. This is working perfectly.
3) Where I'm stuck is how to exclude that featured post from the query so we don't get duplicates. I'm trying to do this in a pre_get_posts function in functions.php to modify the query so it functions like a normal archive page otherwise.
If you know of an easier way to simply set a featured article for a category I'm open to suggestions. I know sticky for main homepage loop but nothing for archive pages.
You may need more info but that should be a good starting point. Using ACF Pro (latest)
Thanks for the help!
You can use the ignore_sticky_posts property while querying posts using WP_Query to exclude the sticky/featured posts.
Reference: WP_Query

Woocommerce: Customizing the availability of a category

I have many products nested within parent categories in WooCommerce.
I want to be able to apply a custom field where I can specify a time availability for categories as a admin feature only in Back end.
Theory:
Back end - On each parent category I will have this custom field where I can type or choose a time frame: for example 11:00-17:00.
Front end:
During this time frame, users will be able to browse all the products within that category.
Outside of this time frame all the products within that category will stay visible (but inactive) and they will be displayed greyed out or with a 'closed' banner (something like that).
Where can I start with this?
THE BEGINNING
You can start adding 2 plugins to add a custom time picker on product categories admin pages:
Advanced custom fields (ACF) plugin
Date & Time Picker for Advanced Custom Fields or also Date and Time Picker Field
This is just he beginning (and I can't confirm that is the best way). So In ACF you create a group and you name it:
Then you set the location (It's going to be a "taxonomy term" for "Product categories"):
Then In options you can chose it like a Metabox on the side (on backend):
After you add a new field and name it. You chose the field type and time only option:
[
Then now you are going to compile all the other options to fit your needs. Then you can save and add, if you need a 2nd field…
Now if you go to the WooCommerce Products Menu, under categories submenu, you will get this:
Now you can read a little the ACF documentation and also the wooThemes/woocommerce dev. documentation too.
You can customize WooCommerce overriding the templates and use hook filter and actions.
You will find in here on Stack OverFlow and over internet a lot of code, examples and tutorials, to begin and try to achieve your project. When needed you will post questions to get helped, with the code you are using telling what is working and what not…

Custom fields button on category pages

I have looked into http://codex.wordpress.org/Custom_Fields. And know how to target custom posts and output them on a page.
Now i need to create custom fields in all posts inside category.php and inside single.php.
I will make fields to be two buttons and later that will link to other pages.
However i can't find answer how i am supposed to add custom fields to specific category posts. Ofcourse every post with buttons will have a unique link.
Could anyone point me in the right direction how i am supposed to create custom fields in all posts of a specific category?

How do I sort custom taxonomy pages by custom date field in wordpress?

What I'd like to do is sort all posts that appear on pages filtered by different children of taxonomies, by a 'date' meta key.
I found this generic query_posts function:
query_posts('&meta_key=date&orderby=meta_value&order=DESC')
This grabs all the posts and places them on a single page, sorting these by my custom meta Key. I would like to still keep the pages separated by their given taxonomy/terms, but sort all of these by this date meta_key. I hope I've explained this correctly. Thank you very much, anyone who can help!

Resources