Get all posts by multiple categories - wordpress

I want to get all posts by specifying multiple categories from drop-down list. In
pre_get_posts i want to modify the query to search by selected categories(more than one).
$taxquery = ($taxquery, array(
'taxonomy' => 'cat',
'field' => 'id',
'terms' => array('1','2'),
'operator'=> 'IN'
));
$query->set( 'tax_query', $taxquery );
But when I execute this code, non of the posts are shown(I am sure that there is posts in at least one of that two categories)
Maybe there is better way for searching by more than one category by using OR when search by the first,second... categories.
Any directions would be great!

First you should use term_id, instead id, and then you should add additional array to your tax_query, because you nay have more than one. So your code will look like this:
$category_ids = array(1,2);
$query->set( 'category__in', $category_ids );

After hours of debugging I finally found the solution...
There was several mistakes as: Ids needs to be Integers in terms array, taxonomy needs to be 'category' not 'cat', in my case the relation needs to be OR as I want to search by multiple categories and by other custom field, and at least one category needs to be meet...
Here is example of the code:
$taxquery = array(
'relation' => 'OR',
'post_type' => 'post',
'tax_query' => array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array(1,2,3),
'operator'=> 'IN'
));
$query->set('tax_query', $taxquery);

Related

Get Posts based on serialized meta value

I have a series of posts and all have a meta_key with the value of "owner" and meta_value with serialized data such as "a:3:{i:0;s:3:"325";i:1;s:2:"41";i:2;s:2:"29";}"
meta_key owner
meta_value a:3:{i:0;s:3:"325";i:1;s:2:"41";i:2;s:2:"29";}
I am trying to figure out how to properly use get_posts() to return all the posts that have a meta_key with the value of owner and the meta_value containing a specific value such as 41
$args = array(
'post_type' => 'rp_applications',
'nopaging' => true,
'meta_query' => array(
'relation' => 'AND',
array(
'compare' => '=',
'key' => 'archived',
'value' => '0000-00-00'
),
array(
'compare' => '=',
'key' => 'owner',
'value' => ???? WHAT DO I PUT HERE ????
)
)
);
$applications = get_posts($args);
This sounds like you should be storing the "Owner" as a taxonomy term instead of meta data. It would give you easier and faster querying abilities as well.
That said, it's definitely possible to do what you want. I'd be cautious though, because the postmeta table by default is not indexed, so a large table with meta queries run against it will slow down your site quite a lot. You may want to consider adding a partial index to the table if it's going to be even remotely large (or again, switch to Taxonomy Terms instead of post meta for this).
If you still care to use a meta query, take a look at the compare options available in the WP_Meta_Query() docs.
One of the available options is REGEXP, so you could do something like:
$args = array(
'post_type' => 'rp_applications',
'nopaging' => true,
'meta_query' => array(
'relation' => 'AND',
array(
'compare' => 'REGEXP',
'key' => 'owner',
'value' => sprintf( 'a:\d:{i:0;s:\d:"\d.*?";i:1;s:%d:"%d".*', strlen($owner_id), $owner_id )
),
array(
'compare' => '=',
'key' => 'archived',
'value' => '0000-00-00'
)
)
);
Of course this method only works if the serialized data is in the same format on each one, and you'd need to adjust the regular expression if not - but that should be enough to get you started!

WordPress: one condition for multiple values but one key when querying with custom fields

I am working on some kind of booking extension for a custom post type in Wordpress. The admin can choose the dates when the object is booked. The booked date is saved as a new custom field with the key "booking" and the date in the format "yymmdd". So one object can have multiple custom fields with the same key, but multiple values.
Now I need to query the objects that are available for a specific date. The following is one of my last attempts.
$args = array(
'post_type' => 'object',
'meta_query' => array(
array(
'key' => 'booking',
'value' => array('131020', '131021', '131022'),
'compare' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );
From my logic, I am searching for an object that is available from 131020 to 131022. What WordPress sees to do though is comparing each of the available multiple values individually. So if the element is booked on 131019 and on 131020, WordPress still returns it as available, because 131019 is not in the values above.
What I need is a logic that say "if none of the multiple values is among the mentioned numbers, return the post"
I also tried using just a serialized array for all of the dates, but this didn’t work either. I am sure there might be a simple trick, but after doing a lot of research I am stuck here.
Any ideas?
Thanks
After many other approaches, I finally ended up with the following "solution".
I saved all the dates as an array and was using NOT LIKE for each of the dates that needed to be available. The code from above would look like this:
$args = array(
'post_type' => 'object',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'booking',
'value' => '131020',
'compare' => 'NOT LIKE'
),
array(
'key' => 'booking',
'value' => '131021',
'compare' => 'NOT IN'
),
array(
'key' => 'booking',
'value' => '131022',
'compare' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );
Note relation => 'AND' at the beginning. As I understand, this is the default, but in case you use 'OR' here this wouldn’t work either.
Still happy about any comment on both of the approaches.

Get multiple posts by multiple slugs

I have an array with multiple slugs (post names) and I want to get all posts where the slug is in the array.
I use Event Organsier and want to get all events these slugs. I haven't worked with WordPress for a couple of years so I need some help.
$events = eo_get_events(array(
'numberposts'=>5,
'event_start_after'=> $year.'-'.$month.'-'.$list_day,
'event_end_before'=> $year.'-'.$month.'-'.$list_day,
'showpastevents'=>false,//Will be deprecated, but set it to true to play it safe.
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array('slug-example-1', 'slug-example-2'),
'operator' => 'IN'
)
)
));
This should (imo) return all posts that has a slug that is slug-example-1 or slug-example-2.
I don't even know if tax_query/taxonomy is the right thing to use.
You can use the post_name__in argument in conjunction with get_posts(), it's available since Version 4.4.
E.g. :
$args = [
'post_name__in' => [ 'slug-1', 'slug-2' ],
'post_type' => 'page',
'post_status' => 'publish',
];
$result = get_posts( $args );

Advanced custom fields filtering with taxonomies

I'm working with AdvancedCustomFields and Taxonomies, and I don't find, in the documentation, something that allows me to filter the Taxonomies by the values stored on the wp_options table.
I found that here.
And, it could by something like this, but with Taxonomies:
I have a taxonomy called "Person", and I have many fields. For example, I would like to filter by sex and country.
Is there any function that allows me to do this? Or should I work with WP-Query?
Thanks in advance
Once you have registered the taxonomy with your post you may try with tax_query Query inside the your query_post
query_posts( array(
'post_type' => 'your post type',
'paged' => $paged,
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'person', //or tag or custom taxonomy
'field' => 'id'
)
)
) );

Only show WordPress 'standard' post format on template?

I recently added post formats to my WordPress theme - on the blog page its fine as they are all styled accordingly. However on my home page template I only want to show 'standard' posts formats (no links, galleries, audio, video etc.).
In my theme options I can decide how many posts to display on the front page which is what 'dft_recent_number' is for.
Does anybody know how I can change the code below to exclude all but 'standard' post formats?
<?php
$query = new WP_Query();
$query->query('posts_per_page='.get_option('dft_recent_number'));
//Get the total amount of posts
$post_count = $query->post_count;
while ($query->have_posts()) : $query->the_post();
?>
Any help is much appreciated!
WP_Query does not seem to have a straightforward parameter for post_format.
From my quick research it appears that post formats are related through taxonomies. So, theoretically, using the taxonomy parameters should work.
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => 'post-format-standard',
)
)
);
$query = new WP_Query( $args );
Note: you'll need to update the taxonomy names and slugs for your blog. This should be the names you set in your functions.php file.
// there's no post-format-standard so you should write it like this to exclude all other postpformats
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array('post-format-quote','post-format-audio','post-format-gallery','post-format-image','post-format-link','post-format-video'),
'operator' => 'NOT IN'
)
I know that's old, but I was facing the same issue and even though I've found a solution I wondered what other have done to "fix" that.
I think a more scalable solution could be something like that:
$post_formats = get_theme_support( 'post-formats' );
$tax_query = false;
if ( $post_formats ) {
$tax_query = array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => $post_formats[0],
'operator' => 'NOT IN'
)
);
}
// WP_Query arguments
$args = array(
'post_type' => 'post',
'order' => 'DESC',
'orderby' => 'date',
'tax_query' => $tax_query
);
This would exclude the post formats that have been enabled and also will work in case WP will add more post formats (or add the ability to add more).

Resources