Wordpress Query to fetch both posts and pages by a custom field/key - wordpress

I am working on a slider where I will show some contents(featured image and excerpt) pulled from both posts and pages. I want to filter the posts/pages by a custom field called 'slider'!
So if there are some pages and posts with 'slider' custom field only those will appear in the slider. Is it possible in wordpress? If it is then how?
Some guidance will be appreciated!

What you're looking for is the class called WP_Query, has got a detailed explanation in the Codex. Take a look at the post_type argument which accepts an array, thus you can give it a array( 'post', 'page' ) or any other post types that you want to fetch.
Now the meta fetch could be done in two ways, either via the new meta_query argument (from 3.1 onwards I believe) or meta_key and meta_value which are deprecated since 3.1.
Here's a rough example (haven't checked if this works):
$sider_posts = new WP_Query( array(
'post_type' => array( 'post', 'page' ),
'meta_query' => array(
array(
'key' => 'slider',
'value' => 'yes',
'compare' => '='
)
)
) );
while ( $slider_posts->have_posts() ) {
$slider_posts->the_posts();
// output the slide here
}
Hope that makes sense. Cheers!
~ K

Related

Using wordpress and advanced custom fields, how can I use a subfield for my meta query?

I'm using Wordpress with the Advanced Custom Fields Pro plugin and I'm trying to get all the posts that have a date field that is after today's date. I looked at this example https://www.advancedcustomfields.com/resources/date-picker/ and that works fine except that my field is a subfield of a repeater field. And I have some trouble making a meta query for that. I tried using _%_ for the subfield but then I read here https://support.advancedcustomfields.com/forums/topic/meta_query-for-a-group-field-sub-field/ that I can just use an underscore for the subfield. But posts still aren't coming up with any results. I tried checking the date from just a single post and it appears to be working correctly. I also read here advancedcustomfields.com/resources/date-picker that the date is always stored as Ymd in the database. I tried doing it with another field that isn't a subfield and that worked, so the problem lies with the subfield I think 'key' => 'date_startdate',
I am using the date picker as a subfield.
function do_get_upcoming($atts = [])
{
$defaultLimit = 8;
$limit = $atts["limit"] != null ? $atts["limit"] : $defaultLimit;
$today = date('Ymd');
$posts = get_posts(array(
'numberposts' => $limit,
'post_type' => 'events',
'meta_query' => array(
array(
'key' => 'date_startdate',
'value' => $today,
‘type’ => ‘DATE’
'compare' => '>',
)
),
));
echo count($posts);
}
add_shortcode('get_upcoming','do_get_upcoming');
screenshot subfield
I found the solution. Subfields can be accessed by adding _0_ so you'll get 'key' => 'date_0_startdate'

Wordpress filters

I used wordpress to make a website with some custom types. The problem is that i need to implement a filter for these types and I have some questions about the plugins that i'm using for this project:
For to make custom types and views I'm using Types and Views plugins for wordpress.
I made a new custom type with some fields (title, price, some selectors, ...) and I need to filter all the information ordering the results following 2 criterias. I don't know if is it possible to do so using the Views/Types plugin.
I also need to filter the fields of the filter. For example, if i have 2 fields one called Countries and other called Cities, if i select Spain in countries I should see only spanish cities in the second field.
Any idea if i can do that using Types and Views plugins? is there any other plugin?
Thanks in advance!
Any good plugin will save the custom fields as post-meta. You need to find out what the key is for the field you are trying to use as a filter. Once you have that you can use a simple meta query to get the posts you need:
$args = array(
'post_type' => 'post', // The post type
'posts_per_page' => -1, // Get all posts
'orderby' => 'rand', // Random order, also can use 'post_date' or 'post_title'
'order' => 'ASC', // Direction of sort
'meta_key' => 'my_meta_key', // Whatever your key is
'meta_query' => array(
array(
'key' => 'my_meta_key', // Whatever your key is
'value' => 'foo' // What it should be
'compare' => 'IN', // How to compare the value to the stored value
)
)
);
$query = new WP_Query($args); // The query
Here is more on the Wordpress codex.

How to create/modfiy WP_Query to search in post title OR custom field?

I need to modify or create a WP_Query that will search for a search term in both the post title OR a custom field (called 'my_field').
I have been reading and trying for hours, but I am right back to this code (below) which, alas, only searches in 'my_field' and does not take the post_title into account.
function my_pre_get_posts_2( $query ) {
if ( is_admin() && $query->is_main_query() && $query->query['post_type'] === 'post' && isset($query->query['s']) ) {
$search_word = $query->query['s'];
$args = array(
//'s' => $search_word, //If I include this line, the WP query seems to AND post_title and my_field. If I comment out this line, the WP query only searches in my_field. (I need WP to OR post_title and my_field.)
'post_type' => 'post',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'my_field',
'value' => $search_word,
'compare' => 'IN'
),
array(
'key' => 'post_title',
'value' => $search_word,
'compare' => 'IN',
)
)
);
//$query = new WP_Query( $args ); //Need to modify the existing WP_Query
$query->init();
$query->parse_query($args);
}
}
add_action( 'pre_get_posts', 'my_pre_get_posts_2' );
The reason I need to do this is because I need to modify the behaviour of the the 'Search Posts' button in the 'All Posts' (admin) page so that whatever the admin user searches for, it will return the posts that have a matching post title OR my_field value.
To do an OR search, I tried merging two separate WP_Query results as shown here - https://wordpress.stackexchange.com/questions/55519/can-i-merge-2-new-wp-queryvariable-s - in guidod's answer. That wasn't a great solution though, and resulted in erratic behaviour.
The correct solution I found was to modify the query using the WP Custom Query as shown in the code (which requires some modifications) here - http://codex.wordpress.org/Custom_Queries .

How to display all posts of specific custom post types, including normal posts?

I want to display all posts of 2 specific custom post type (causes and evenements), but I also want to include all normal posts. Here's my query :
$args = array(
'post_type' => array( 'causes', 'evenements', 'post' ),
'posts_per_page' => -1,
);
$my_query = null;
$my_query = new WP_Query( $args );
Unfortunately, this only returns normal posts.
When I use this query, it shows all posts of causes and evenements custom posts :
$args = array(
'post_type' => array( 'causes', 'evenements' ),
'posts_per_page' => -1,
);
How can I display all posts of causes, evenements and post post types?
I can't use any value for the post_type because I also have other custom post types that I don't want to include in my query.
Thanks for the help!
Better to use this plugin Ultimate "Category Excluder"
It is simple to use and the ui provided is user friendly.

WordPress get user by meta data

How can I retrieve all users registered in my WordPress blog having a particular meta data?
For example I have made an option to add a custom meta data for every registering users having meta key as parent_id. If I want to list all users having parent_id as 2 , then how can I do this?
Since WP v3.1 it's ridiculously easy to search for a user by his/her meta key.
Use the function
get_users($args)
(WP Documentation)
The function takes an array of parameters, in your case you need
get_users(array(
'meta_key' => 'parent_id',
'meta_value' => '42'
))
Simple way how to get one user by his metadata is:
$user = reset(
get_users(
array(
'meta_key' => $meta_key,
'meta_value' => $meta_value,
'number' => 1
)
)
);
Here is how you can get users based on a custom role and multiple metadata keys,
$available_drivers = get_users(
array(
'role' => 'driver',
'meta_query' => array(
array(
'key' => 'approved',
'value' => true,
'compare' => '=='
),
array(
'key' => 'available',
'value' => true,
'compare' => '=='
)
)
)
);
Explaining the above query, I want only those users who I assigned the role of driver, and they are approved and available. The approved and available are custom fields created using ACF as True/False fields.
If you have additional metadata to test, add another array element to the meta_query array.
Meanwhile checkout my open source at github.com/patrickingle
Here is the codex page from Wordpress detailing how to use the get_users($arg); function.
It contains examples how how to build custom functions to fetch various parts of user data. You'll have to naturally build and make some of your own changes to get it how you want.
Additionally here is a link to a function somebody built that will fetch user data based on roles within wordpress. You can configure it in many different ways with some tweeking, but this will allow you to filter your results in a more powerful manner.

Resources