Calling a Custom Field Value in the Wordpress Loop? - php

I am trying to call a custom field's metadata, and want to use it as a flag field for a Custom Post Type's Loop for a page. The field is 'tt_freemium'. The code I have below pulls everything and ignores the flag field. Uuuugh. What am I doing wrong ?
<?php $args = array( 'post_type' => 'membercontent', 'tt_freemium' => 'true', 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => '200' );
$ourposts = new WP_Query( $args );?>

The answer in case anyone wants to know is to add a meta_query and an array to fill the meta query. It works now. Sorry to bother anyone that read this. ;-) Have a nice day y'all.
<?php $args = array(
'post_type' => 'membercontent',
'meta_query' => array(
array(
'key' => 'tt_freemium',
'value' => 'true',
),
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => '200' );
$ourposts = new WP_Query( $args );?>

Related

How to query posts by ACF field but then also order by a separate ACF field?

I am using WP_Query to get posts that have a specific value in one of the ACF fields. I also need to order them by a separate ACF field. I am not sure how to accomplish this. Everything I've read says to use 'orderby' => 'meta_value' but I believe thats the value of the field I'm filtering the posts by, which is not what I want.
This is what I have right now..
$args = array(
'post_type' => 'contacts',
'posts_per_page' => -1,
'meta_key' => 'department',
'meta_value' => 'Transportation',
'orderby' => 'meta_value'
);
$the_query = new WP_Query( $args );
I need to orderby an ACF field named last_name.
It's possible to assign a name to a meta query, and then refer to that name in your orderby. Something like this.
$args = array (
'post_type' => 'contacts',
'post_status' => 'publish',
'nopaging' => true,
'posts_per_page' => -1,
'meta_query' => array( 'main_query' => array(
'key' => 'department',
'value' => 'Transportation'
), 'orderby_query' => array(
'key' => 'last_name',
)
),
'orderby' => array(
'orderby_query' => 'ASC',
),
);
$the_query = new WP_Query( $args );

WordPress WP_Query: Display custom post type based on custom meta value, and also order on another custom meta value

I am using the WordPress plug-ins Advanced Custom Fields, and Custom Post Type UI.
I have built a WP_Query to display a particular post type, filtered by a particular custom field value.
$loop = new WP_Query( array(
'post_type' => 'news',
'meta_key' => 'news_story_type',
'meta_value' => 'release',
'posts_per_page' => 3
) );
I now want to sort the resulting posts by another custom field, date_of_publication rather than use WordPress's menu_order or date. The ACF documentation says to specify orderby and meta_key in the query args.
$args = array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'DESC' );
But alas, doing so conflicts with the meta_key I've already supplied to filter.
Has anyone encountered this before and found a solution?
Try using meta_query
$loop = new WP_Query( array(
'post_type' => 'news',
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'posts_per_page' => 3,
'meta_query' => array(
array('key' => 'news_story_type', 'value' => 'release')
)
) );

Wordpress loop for multiple custom post types

Looking for a way to loop through multiple custom post types and get the 4 most recent posts.
I have the below but doesn't quite work as if you 2 posts of the same post type, it'll show the most recent rather than showing most recent of ALL custom posts.
$args = array('post_type' => array('cs_trainee', 'cs_graduates', 'cs_pros', 'sd_trainee', 'sd_graduates'), 'posts_per_page' => 4, 'orderby' => 'menu_order', 'order' => 'ASC');
$careers = new WP_Query( $args );
Thanks
Just change the query_posts parameters a bit....
query_posts( array(
'post_type' => array( 'post', 'report', 'opinion', bookmark' ),
'cat' => 3,
'orderby' => 'date',
'order' => 'DESC',
'showposts' => 5 )
);
That should take care of it for you.

Enable the order meta box

How do I enable the order meta box? I want it to set the order.
<?php $loop = new WP_Query( array( 'post_type' => 'bocker', 'posts_per_page' => 10, 'product_category' => 'spanskt', 'orderby' => 'meta_value', 'order' => 'ASC', ) ); ?>
This link should have the information you need: http://codex.wordpress.org/Function_Reference/add_meta_box
There's also a tutorial on this subject here:
http://wptheming.com/2010/08/custom-metabox-for-post-type/
This post solved my problem: order posts by custom field
I made a custom field instead (which suited this even better than a metabox) named "pub_year":
<?php $loop = new WP_Query( array( 'post_type' => 'bocker', 'posts_per_page' => 10, 'product_category' => 'ex-jugoslaviskt', 'meta_key' => 'pub_year', 'orderby' => 'meta_value', 'order' => 'DESC' ) ); ?>

Wordpress query by multiple metaboxes and order by date

So here is my query:
$args = array(
'post_type' => 'Event',
'posts_per_page' => 1000,
'meta_key' => 'event_informations_show_on_the_homepage',
'meta_value' => 'Show on the homepage',
'meta_compare' => '==',
'meta_key' => 'event_informations_date',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$loop = new WP_Query( $args );
I want to select all posts that have the metabox event_informations_show_on_the_homepage and the value of the metabox event_informations_show_on_the_homepage and order by the date metabox which is stored as a timestamp and is called event_informations_date.
What am I doing wrong?
Hopefully I'm not barking up the wrong tree here.
You can use the key 'meta_query' to filter posts by multiple meta keys like so:
$args = array(
'post_type' => 'Event',
'posts_per_page' => 1000,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'event_informations_show_on_the_homepage',
'value' => 'yes',
),
array(
'key' => 'event_informations_date',
'value' => 'yes',
)
)
);
$query = new WP_Query( $args );
What WordPress is doing here is creating multiple wheres against the same column by using innerjoins on the same table, each time using a different alias. It's pretty cool & is probably the fastest way to query like that.
For more information see here: http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
Hope this helps :)

Categories