How to query by custom post type values in WordPress? - custom-post-type

I have a Custom Post Type using the Custom Post Type UI plugin called Case Studies. I'm also using the Custom Fields to add a capability field to the Case Studies.
How can I query Case Studies where the capability is equal to some ID?
$query = array('post_type' => 'case-studies','posts_per_page' => 3);
is my query so far

It could be
$query = array(
'post_type' => 'case-studies',
'meta_key' => 'capability',
'meta_value' => 10, // some ID
'posts_per_page' => 3
);
$the_query = new WP_Query( $query );
while ( $the_query->have_posts() ) : $the_query->the_post();
// echo here
endwhile;
wp_reset_postdata();

You need to set your key to capability, and then query value by your post ID.
'meta_query' => array(
array(
'key' => 'capability',
'value' => $post->ID,
'compare' => 'example'
)
)

Related

Select specific custom post type from selected taxonomy on wordpress

hello i have post_type "product" and taxonomy "price" can someone help me to query that custom post type with the selected taxonomy?
my current code is like this
<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args = array(
'post_type' => 'product',
'posts_per_page' => 5,
'tax_query' => array(
array(
'taxonomy' => 'productcategories',
'terms' => $term->name
)
)
);
query_posts($args);
print_r($args);?>
but nothing on result, can someone help me? thank you
i'm found what wrong already.
nothing wrong with the code, i'm already solved it. that's because i'm not set the content into selected custom category yet, that why it display null.
my bad

extend wordpress search query

I have a working search query with offset and post_per_page (the user can go through pages)
$args = array(
'post_type' => 'type',
's'=>$the_str,
'posts_per_page' => $the_count,
'offset' => ($the_count*$the_c_page )-$the_count
);
$the_query = new WP_Query( $args );
It works great.
But now I need to add a meta field to the search function. It should get all post where s=>$query OR meta-field=>$query
Something like this:
$args = array(
'post_type' => 'type',
'posts_per_page' => $the_count,
'offset' => ($the_count*$the_c_page )-$the_count,
'meta_query' => array(
'relation' => 'OR',
's'=>$the_str,
array(
'key' => 'key',
'value' => $the_str
)
);
$the_query = new WP_Query( $args );
Unfortunately this will only search after the second condition. Does anybody have an idea?
If you crack open query.php you'll see that it really isn't built with this in mind. It isn't that it isn't a great idea, it's just that each individual query part is concatenated onto the main WHERE using an AND. So "title contains XYZ" AND "whatever the meta query is" AND "whatever the tax query is", etc. Meta and Tax both have sub logic within them that supports AND and OR but there's no way to perform this between these outer parts.
However, you might be able to do what you're looking for by tapping into one of the filters, possibly get_meta_sql. The meta query returns a statement that puts in the AND that you want to be an OR so you can just look for that and replace it:
function change_and_to_or_for_meta( $meta_query, $type, $primary_table, $primary_id_column, $context )
{
if( 0 === strpos( $meta_query['where'], ' AND' ) )
{
$meta_query['where'] = ' OR' . substr( $meta_query['where'], 4 );
}
return $meta_query;
}
//Add our filter to replace the AND at the start with OR
add_filter( 'get_meta_sql', 'change_and_to_or_for_meta', 10, 5 );
$query = new WP_Query( $args );
//Remove our filter so we don't mess other things up
remove_filter( 'get_meta_sql', 'change_and_to_or_for_meta', 10 );

Wordpress custom post type archive page wp_query pagination not working

I have created an archive page for my 'Event' custom post ape called archive-event.php.
I used the content from the standard archive.php template and it listed my 4 events in the order they were posted.
The next step was to change the order by the custom field event_date. I did this no problems. Then I wanted to not show any events that event_date had passed. The below code does this perfectly.
The issue I now have is my pagination is all messed up. I have the default reading settings set to 2 posts per page and I have a 'Load more' button to bring up the next two.
When I click 'Load more' it duplicates the two events already showing. Any ideas where I've gone wrong?
<?php
$today = date("Ymd");
$args = array (
'post_type' => 'event',
'meta_key' => 'event_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_date',
'value' => $today,
'type' => 'numeric',
'compare' => '>=',
)
),
);
$wp_query = new WP_Query( $args );
while( $wp_query->have_posts() )
{
$wp_query->the_post();
?>
<div>Content goes here</div>
<?php } wp_reset_postdata(); ?>
Depending on how the plugin builds the UI it may or may not be respecting the pagination functionality built into WordPress.
Try forcing the paged setting into your query like so:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array (
'post_type' => 'event',
'paged' => $paged, // this will be set to what WordPress thinks is the proper page to start
'meta_key' => 'event_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_date',
'value' => $today,
'type' => 'numeric',
'compare' => '>=',
)
),
);
Next, if this still doesn't work, you can test the assumption that the paged parameter isn't working for some reason by changing the default paged value like so (again, just to test):
$paged = 2; // just force it to start on the second page to see if that works
If the above test works, I would say that get_query_var() believes you're on the first page.

How do I add an Excerpt box to custom post types in functions?

I just want the standard Excerpt box - not a metabox of my own creation, added to a Custom Post. The box shows up in Posts but not in Custom Posts. I've tried both of these older solutions but neither of them worked (maybe it's a WP 3.9 problem):
The custom post type name is "Scoop"
I added this to the register_post_type_scoop() $labels = array
'supports' => array('title','thumbnail','excerpt')
but it didn't work - neither did this:
add_post_type_support('Scoop', 'title');
add_post_type_support('Scoop', array('title', 'thumbnail', 'excerpt') );
Add a index value excerpt to the supports object. Below the example is:
add_action( 'init', 'create_testimonial_posttype' );
function create_testimonial_posttype(){
register_post_type( 'testimonials',
array(
'labels' => array(
'name' => __( 'Testimonials' ),
'singular_name' => __( 'Testimonial' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'clients'),
'supports' => array('title','thumbnail','editor','page-attributes','excerpt'),
)
);
}

How to limit pagination?

I have a problem with this pagination i use this code
var $paginate = array(
'limit' => 5)
yes it work but after i code my search plugin, this pagination code is not working
please help me
Try using this as an example for your pagination
$this->paginate = array(
'conditions' => array( ... )
'limit' => 5,
);
$myResults = $this->paginate( 'MyModel' );

Resources