This is what I have to query all events...
query_posts("post_type=marcato_show&meta_key=marcato_show_start_time_unix&orderby=meta_value&order=ASC&posts_per_page=999" );
and this works, just getting the community events...
query_posts( "post_type=marcato_show&showtype=community-events&meta_key=marcato_show_start_time_unix&orderby=meta_value&order=ASC&posts_per_page=999" );
how do I go about getting all the events WITHOUT the community events?
Rather than using query_posts(), which can cause a number of issues (see here and here), I'd recommend using the WP_Query class.
This will avoid some obscure bugs, speed up the query for you, and also make it easier to make changes like excluding posts assigned to a certain term (in this case, the community-events term of the showtype taxonomy).
Be sure to read up on the WP_Query documentation as it covers a lot of useful arguments you may need in the future. But for now, here's how you'd run your same query, excluding those posts in community-events:
$posts = new WP_Query(array(
"post_type" => "marcato_show",
"meta_key" => "marcato_show_start_time_unix",
"orderby" => "meta_value",
"order" => "ASC"
"posts_per_page" => 999, // you can also use `-1` to return unlimited results
"tax_query" => array(
array(
"taxonomy" => "showtype",
"field" => "slug",
"terms" => "community-events",
"operator" => "NOT IN",
),
),
));
if($posts->have_posts()){
while($posts->have_posts()){
$posts->the_post();
// DISPLAY YOUR POSTS HERE
}
wp_reset_postdata(); // restore original post data
This is querying with the same arguments you used in your original post, with the added tax_query to query posts not in that custom taxonomy term. Scroll down to Taxonomy Parameters at the WP_Query documentation linked above for full details on how this works.
Related
So I'm looking to include the functionality of displaying content by its custom fields as well as its title and content.
I need to be able to search for orders, as well as subscriptions, on WooCommerce by custom field as well as the normal method. Is there any way I can, without adding additional search forms or booleans, simply get Wordpress to display posts that match with the search term by their custom fields, too?
I have used the following code thanks to a responder on here:
function custom_search_query( $query ) {
$custom_fields = array(
// put all the meta fields you want to search for here
"gender",
"birthdate"
);
$searchterm = $query->query_vars['s'];
// we have to remove the "s" parameter from the query, because it will prevent the posts from being found
$query->query_vars['s'] = "";
if ($searchterm != "") {
$meta_query = array('relation' => 'OR');
foreach($custom_fields as $cf) {
array_push($meta_query, array(
'key' => $cf,
'value' => $searchterm,
'compare' => '=='
));
}
$query->set("meta_query", $meta_query);
};
}
add_filter( "pre_get_posts", "custom_search_query");
This works great when searching orders, but what I need to search is subscriptions, where it doesn't work.
Help would be much appreciated!
I have a custom post tipe ("artigo"), and this CPT has a registered taxonomy ("artigo_eixo"), with 3 registered terms in it.
I want to show, in that page, a sidebar listing all terms, except the current term, in the page that lists all posts with that term (http://localhost/mytaxonomy/current_term/)
However, I can't exclude the current term from the query.
I'm trying to get a list of taxonomy terms, but exclude a certain term from this list. I'm trying to follow the codex examples, but I'm having no success, using either get_terms() or WP_Term_query().
There are 3 terms and the IDs are 13,14,15. There are no posts or CPT items with those IDs.
I just tested get_queried_object_id() and it seems to be returning the proper term IDs of the term being shown -- that is, if I am viewing the URL of the term with ID 13, the function returns 13, and so on.
It also doesn't work using hardcoded values, be it a string, an integer, or an array of any of those types. Neither won't work:
'exclude' => 14
'exclude' => '14'
'exclude' => array(14)
'exclude' => array('14')
There are no errors displayed by PHP or the WP debug log.
get_terms
if (is_tax( 'artigo_eixo' )) {
$current_eixo = get_queried_object_id();
$args = array(
'taxonomy' => 'artigo_eixo',
'exclude' => $current_eixo
);
$eixos = get_terms( $args );
WP_Term_query
if (is_tax( 'artigo_eixo' )) {
$current_eixo = get_queried_object_id();
$args = array(
'taxonomy' => array('artigo_eixo'),
'exclude' => $current_eixo
);
$eixos = new WP_Term_Query( $args );
$eixos = $eixos->terms;
Turns out there was a function hooking the filter list_terms_exclusions, and the return was inside a logically faulty conditional. I fixed it and now the term query exclusion works as intended.
How do I display a list of posts belonging to a particular term? I have a particular use case where I do not know the taxonomy to which that term belongs to.
Had I known the taxonomy, I would have used the tax_query parameter of the get_posts() function.
So, I tried to find the taxonomy name from term name by performing a database query based on this WPSE question. But then, I need the term_taxonomy_id for this to work. I just have the name of the term.
So, I tried finding the term_taxonomy_id using the term name. To achieve this, I came across the get_term_by() function. Unfortunately, this function requires the name of the taxonomy to work. This brings me back to the point where I started.
My primary problem of displaying a list of posts belonging to a particular term when the taxonomy name is unknown remains unsolved. I'm looking for pointers in this direction.
Save term IDs instead of names. Not that users need to select them, but for whatever term names selected save its ID as information meaningful for code instead.
Since recent WP versions no longer do shared terms mayhem, it should be perfectly sufficient to work with:
$term = get_term_by( 'slug', 'aciform', 'category' );
$id = $term->term_id;
// now let's do this backwards from ID alone
$term = get_term( $id ); // WP_Term... taxonomy argument optional!
// and query posts using term object
$posts = get_posts( [
'tax_query' => [
[
'field' => 'term_id',
'terms' => $term->term_id,
'taxonomy' => $term->taxonomy,
]
],
] ); // array(1)
I'm customizing the admin on a new WP site, and I am creating an over-arching custom post type, that will allow the admin to create pages for the site. I want them to be able to pull values from other custom post types and set them here. I'm struggling to find good documentation on how to do this though. I can create multiple custom post types without a problem, just unsure how to pull the values of post_type_y into the meta_options for post_type_x.
Any and all help is appreciated!
Since I can't 'comment' on your question, I'll do my best to answer it as I understand it..
There's a plugin posts to posts that will allow you to associate Post 1 with Post 2. It's a little clunky, but gets the job done.
If you're looking to associate tags, categories or whatever between multiple post-types, I prefer using custom taxonomies as they are relatively easy to implement.
Sample Custom Taxonomy:
function languages_init() {
// create a new taxonomy
register_taxonomy(
'languages',
array('post','clients','positions','projects'), // Set various post-types
array(
'label' => __( 'Languages' ),
'sort' => true,
'args' => array( 'orderby' => 'term_order' ),
'rewrite' => array( 'slug' => 'language' )
)
);
}
add_action( 'init', 'languages_init' );
I have a custom field with meta_name is product_id. I am trying to determine how to search that field when using the wordpress search.
I know the wordpress search results page uses query_string which contains the "s" value for the keyword searched I just don't know how to change my code below to search the custom field mentioned.
$loop = new WP_Query("$query_string . '&posts_per_page=-1&post_type=product'"); ?>
think this section in the codex should have the answers you need with regards to adding in custom field parameters to you query:
http://codex.wordpress.org/Function_Reference/WP_Query#Custom_Field_Parameters
Not sure whether that will allow you to search within a particular custom field though. Can't be certain but I think you'd have to make some changes on the search form end of things. See how you get on with adding in the custom field parameters.
Off the top of my head I would think you would need something like:
$query = new WP_Query( array(
'meta_key' => 'product_id',
'meta_value' => $query_string,
'posts_per_page' => '-1',
'post_type' => 'product'
) );
Not tested though!