WP_Query arguments setting for ID - wordpress

I would like to do the WP_Query with the arguments setting as below, plus "WHERE ID > $post_id" (specific post id).
Pls advise how to add this WHERE clause into the $arg setting?
Thanks a lot.
$args = array (
'cat' => $category_id,
'nopaging' => false,
'posts_per_page' => '5',
'order' => 'DESC',
'orderby' => 'ID'
);
$query = new WP_Query( $args );

If you want to query "all post with an ID superior to X", I think you need to do like this :
$post_ids = range(25, 50); //we need an array of post IDs
$args = [
'posts_per_page' => 25, // if you want to limit/paginate the query
'post__in' => $post_ids
];
$posts = get_posts($args);
This will query all post from IDs 25 to 50 included. Pagination may be useful if you have a very large range of IDs to query. source.
Note that this will not generate a WHERE SQL clause, but a WHERE IN. To get a real MySQL WHERE, I think you'll to go with custom SQL query, as WP_Query doesn't provide a lot of operators on the post IDs.

oops, didn't see the > sign
you can use post__in with posts ids as #Mtxz answered above
'post__in' => array( )

Related

ACF Query Posts by Repeater Field Not Empty

This question seems to be unanswered on the internet, perhaps because it is not possible. I simply want to query all posts where the repeater field 'has rows'. i.e.
$args = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'repeater_field',
'value' => '',
'compare' => '=!'
)
);
I know an alternative is to run if statements inside the loop to filter them out, but that messes with some other logic which is based off of the number of posts in the query.
Does anyone have any thoughts on this?
Let's consider you have a Repeater Field labeled My Repeater Field and this repeater contains at least one field labeled A Field on the Repeater.
Assuming you have the default wp_ table prefix, you need to look at the DB's wp_postmeta table to notice that values for this field on the repeater are stored with the meta_key:
NAME_OF_THE_REPEATER_index_NAME_OF_FIELD_ON_THE_REPEATER
So, in our case, if a post has 3 rows on the repeater field, its values will be stored as:
my_repeater_field_0_a_field_on_the_repeater
my_repeater_field_1_a_field_on_the_repeater
my_repeater_field_2_a_field_on_the_repeater
Knowing this, if you want to query all posts having at least ONE row on the repeater, you could do:
$meta_query = [
[
'key' => 'my_repeater_field_0_a_field_on_the_repeater',
'compare' => 'EXISTS',
]
];
$args = [
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => $meta_query,
'post_type' => 'post',
'post_status' => 'publish',
];
$posts_array = get_posts( $args );
Note: As stated on WP Docs, you can only use the EXISTS comparison on WP >= 3.5 and you don't need to specify a value when using the 'EXISTS' or 'NOT EXISTS' comparisons in WordPress 3.9 and up. I'm also assuming you are using PHP >= 5.4 so you can use short array syntax. If not, just replace [] for array().
You can query the wordpress database using the $wpdb object. ACF fields are saved in prod_postmeta on the database so that is where you will run your query. Your meta_value will be the key of your repeater field, so make sure you replace that in the query below. All keys for any ACF field with start out with field_ and then random characters/digits will follow like seen below. Then once you have the post id, you can run get_post() on those post ids. Let me know if you need anything else or have questions.
global $wpdb;
$results = $wpdb->get_results("SELECT post_id from prod_postmeta WHERE meta_value = 'field_534eeaaa74199'");
$echo $results;
This works. I have test it. Only by the "Welcome World" post it doesn't work.
$args = array(
'post_type'=> 'post',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'repeater_field',
'value' => '0',
'compare' => '!='
)
));
$the_query = new WP_Query( $args );
The "repeater_field" is the name of the field, not the field_key. Here is the count of the rows.

Merging two post type in a single WP Query

I want to fetch the data from two different post type within a single query having different arguments for both the post type.I am using following code but how can I combine two results in a single query?
$args = array(
'post_type' => 'post',
'posts_per_page'=> '1',
);
$args1 = array(
'post_type' => 'page',
'posts_per_page'=> '3',
);
$post_query = new WP_Query( $args );
$page_query = new WP_Query( $args1 );
You have two options, either merge the results or run a third query. I always like the latter because then you keep the query object in place which is quite helpful for post counters and pagination.
We need to be smart here as this can really slow things down unnecessary and can become quite expensive, so this is what we will do
Run two very lean, very smart queries with get_posts (more optimized as a normal WP_Query as it breaks pagination which makes it faster). We will also just query the post ID's and not full objects. This will make these queries very fast and very lean. It will be almost like you never did those queries ;-)
Once we have the results from those queries, we can merge the ID's and run a final query to return full post objects which we can use to run a proper loop
Lets look at the code
// Set our defaults to keep our code DRY
$defaults = [
'fields' => 'ids',
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'cache_results' => false
];
// Set query args for query 1
$args = [
'post_type' => 'post',
'posts_per_page' => '1',
];
// Set query args for query 2
$args1 = [
'post_type' => 'page',
'posts_per_page' => '3',
];
$post_query = get_posts( array_merge( $defaults, $args ) );
$page_query = get_posts( array_merge( $defaults, $args1 ) );
// Merge the two results
$post_ids = array_merge ( $post_query, $page_query ); //. You can swop around here
// We can now run our final query, but first mke sure that we have a valid array
if ( $post_ids ) {
$final_args = [
'post_type' => ['post', 'page'],
'post__in' => $post_ids,
'orderby' => 'post__in', // If you need to keep the order from $post_ids
'order' => 'ASC' // If you need to keep the order from $post_ids
];
$loop = new WP_Query( $final_args );
// Run your loop as normal
}

Wordpress: get all posts of a custom type

I have this strange issue. I want to fetch all posts that are of a custom type, here's my snippet.
$query = new WP_Query(array(
'post_type' => 'custom',
'post_status' => 'publish'
));
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
echo $post_id;
echo "<br>";
}
wp_reset_query();
This only gets me 6 of them, while I have more than 50 records matching that criteria in the database. Can anyone tell me where I have gone wrong?
Many thanks!
'posts_per_page' => -1,
Add this to the WP_QUERY array of arguments and it should return all of the posts of this custom post type.
This get all posts of a custom type using get_posts:
$posts = get_posts([
'post_type' => 'custom',
'post_status' => 'publish',
'numberposts' => -1
// 'order' => 'ASC'
]);
The number of posts to return are set under settings > reading
You can pass the number of posts for your query to return using.
'posts_per_page' => 'number of posts'
You should never use:
'posts_per_page' => -1
It slow and not effective, if you are talking about SQL Query speeds. So it is much better to use some large integer.
This is a performance hazard. What if we have 100,000 posts? This could crash the site. If you are writing a widget, for example, and just want to grab all of a custom post type, determine a reasonable upper limit for your situation.
More details here:
https://10up.github.io/Engineering-Best-Practices/php/#performance
It is advisable to use an integer instead of '-1' For example:
'posts_per_page' => 999999,

wordpress get post where category is not equal to blog

i am trying to get the latest post which is not a blog . I know how to get a post by category name but how to get the post which is not equal to a specific category name . Here is the code i use to get for a category name
$query = array (
'category_name' => 'Blog',
'posts_per_page' => 1,
'post_status' => array('publish')
);
As stated in the Codex (http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters) you can use 'category__not_in' and feed it the id of the category you want to exclude.
$args = array(
'category__not_in' => array($id), // Pass in one or more IDs here.
'posts_per_page' => 1,
'post_status' => 'publish', // no need to pass it in a array
'orderby' => 'date' // Since you wanted the latest post, this is default value so not really needed
);
You can find out the ID of the category by hovering the category-link in the WP-backend and go to the category and then check the adress in your adress-bar, it should look something like this:
/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=2
There's a category_not_in query parameter. To use it you'll need the ID, not the slug, of the category you want to exclude from your result.
http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
$BlogObj = get_category_by_slug('Blog');
$query = array (
'category_not_in' => array($BlogObj->term_id),
'posts_per_page' => 1,
'post_status' => array('publish')
);
You may try this:
$query = array (
'category_not_in' => array(get_cat_ID('Blog'),
'posts_per_page' => 1,
'post_status' => 'publish'
);
Think we need to combine answers of #the-alpha and #o-jones here.
$BlogObj = get_category_by_slug('Blog');
$query = array (
'category__not_in' => array($BlogObj->term_id),
'posts_per_page' => 1,
'post_status' => array('publish')
);
Simply you have to change 'category__not_in' instead of 'category_not_in'. Adding this for future users may come around.

Show post based on the value of another custom field

How do you display posts based on the value of one of its custom fields? My code below doesn't seem to work:
$args = array(
'post_type' => 'sample-cpt',
'meta_query ' => array(array('key'=>'cpt_display', 'value' => 1))
);
$samples = new WP_Query($args);
This still returns all posts even if the value of cpt_display is a 0. Am I missing something?
Try this..
Adding the meta_key = key as part of the args,
then include it again in the meta_query array
$args = array(
'post_type' => 'sample-cpt',
'meta_key' => 'key',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'key',
'value' => 1
)
)
);
$samples = new WP_Query($args);
http://codex.wordpress.org/Class_Reference/WP_Query
I found the answer. The value of 'meta_value' needs to be an array not a string. This is a huge deviation from the WP_Query Codex and needs to be fixed.
$args = array(
'post_type' => 'sample-cpt',
'meta_key' => 'cpt_display',
'meta_value' => array(1)
);
$samples = new WP_Query($args);
wp_reset_query();
Now it only shows posts with the custom field value of 1. Looking at the request query, it shows that by using an array, the request gets converted into using the SQL IN() to search for posts instead of '=':
...postmeta.meta_key='cpt_display' AND CAST(postmeta.meta_value AS CHAR) IN ('1'))...
Source: Display custom post type by custom field date range

Resources