I'm trying to create a custom shortcode in Wordpress that will let me place an attribute to specify a category and query within a custom post type called "case_studies". The taxonomy itself is called "case_study_categories" and I've set the default to be "furniture-interior", as you can see.
What I need is for the shortcode to look something like this:
[get_project_gallery order="ASC" tax_query="flooring"]
Ideally, the shortcode attribute I've placed would override the default of "furniture-interior" and instead use "flooring" to display the appropriate posts. Actually, I would think I don't even really need a default defined at all (so it would just get all the case_studies posts if no attribute is specified) but I can't get that to work either.
BTW, the attribute for order works exactly the way I would want - it overrides the defined default. I just can't figure out how to get this done with the $tax_query.
Any help on this would be very much appreciated. Thanks in advance all!
Here's my code:
function project_gallery_function($atts, $content = null) {
extract(shortcode_atts(array(
'order' => 'DESC',
'orderby' => 'date',
'tax_query' => array(
array(
'taxonomy' => 'case_study_categories',
'field' => 'slug',
'terms' => 'furniture-interior'
)
)
), $atts));
$args = array(
'post_type' => 'case_studies',
'post_status' => 'publish',
'posts_per_page' => '4',
'order' => $order,
'orderby' => $orderby,
'tax_query' => array($tax_query)
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<aside class="grid-gallery four-col grid-gap-4">';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$gallery_thumb = get_the_post_thumbnail_url( $page->ID, 'thumbnail' );
echo '<img src="'. $gallery_thumb .'" alt="" />';
}
echo '</aside>';
}
/* Restore original Post Data */
wp_reset_postdata();
}
add_shortcode( 'get_project_gallery', 'project_gallery_function' );
You shouldn't be worrying about the whole entire tax_query unless you plan on actually redoing the whole tax_query. You should consider changing it to just using $terms instead, since that's the only part you're replacing with the shortcode (you don't need to rebuild the whole array).
Secondly, make sure your code is normalized with your spacing and indentation - future you appreciates the forethought!
Lastly, you could consider passing even more variables to the array to make it a bit more extensible - $posts_per_page, $taxonomy etc.
function project_gallery_function( $atts, $content = null ){
extract( shortcode_atts( array(
'terms' => 'furniture-interior',
'field' => 'slug',
'order' => 'DESC',
'orderby' => 'date',
'taxonomy' => 'case_study_categories',
'post_type' => 'case_studies',
'posts_per_page' => 4,
), $atts ) );
$tax_query = array(
array(
'taxonomy' => $taxonomy,
'field' => $field,
'terms' => explode(',', preg_replace('/\s+/', '', $terms)), // Force to array, allowing comma sep values
)
);
$args = array(
'post_type' => $post_type,
'post_status' => 'publish',
'posts_per_page' => $posts_per_page,
'order' => $order,
'orderby' => $orderby,
'tax_query' => array( $tax_query )
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if( $the_query->have_posts() ){
echo '<aside class="grid-gallery four-col grid-gap-4">';
while( $the_query->have_posts() ){
$the_query->the_post();
if( $gallery_thumb = get_the_post_thumbnail_url( $page->ID, 'thumbnail' ) ){
printf( '<img src="%s" alt="" />', get_the_permalink(), $gallery_thumb );
}
}
echo '</aside>';
}
/* Restore original Post Data */
wp_reset_postdata();
}
add_shortcode( 'get_project_gallery', 'project_gallery_function' );
A few other notes, get_the_post_thumbnail_url() returns a falsey value if it doesn't exist, so you should wrap that in an if statement incase the image can't be found for some reason (corrupted, removed, forgot to add, etc). Also you have a $page->ID variable in get_the_post_thumbnail_url() - but don't seem to have a global $page in the function? Make sure that's the variable you want to use there.
Using code like what I've provided will let you do what you want, [get_project_gallery order="ASC" terms="flooring"], or even add multiple [get_project_gallery order="ASC" terms="flooring,something-else,a-third-thing"] - just match up the shortcode attributes with the parameter you want to override: [get_project_gallery order="ASC" terms="flooring,something-else,a-third-thing" posts_per_page="15"]
Related
I'm working on a short code for loop through parents ID and display all child page, but I'm not quite sure how to make the loop and make it more custom.
Here's my code:
add_shortcode( 'home-page-listing', 'get_list' );
function get_list( $atts ) {
ob_start();
$atts = shortcode_atts( array(
'ids' => ''
), $atts );
if($atts['ids']!='')
{
$id_array = explode(',',$atts['ids']);
$homePages = new WP_Query( array(
'post_type' => 'page',
'post__in'=>$id_array,
'order' => 'ASC',
'orderby' => 'post__in',
'posts_per_page' => -1
) );
if ($homePages->have_posts()){?>
<div class="">
<?php while ( $homePages->have_posts() ) : $homePages->the_post(); ?>
//here's html template code
<?php endwhile;
wp_reset_postdata(); ?>
</div>
}
}
}
Right now I can use [home-page-listing id=1,2,3,4] to display all select page ID, but I would like to make like this:
[home-page-listing parentID=4]
loop through all child page and display to the font, instead go check all the page id to display.
Thanks!
it's simple used post_parent Arguments of WP_Query. Please check below example
$homePages = new WP_Query( array(
'post_type' => 'page',
'post_parent'=>$parentID,
'order' => 'ASC',
'orderby' => 'parent',
'posts_per_page' => -1
) );
For more information of WP_Query click here
I want to get 3 featured products in the header of the site. But my query keeps returning unlimited number of results.
I've been looking online for a solution and came across answers that all answer saying the same thing in terms of the query. What could I be doing wrong?
$meta_query = WC()->query->get_meta_query();
$tax_query = WC()->query->get_tax_query();
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
);
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 2,
'meta_query' => $meta_query,
'tax_query' => $tax_query,
);
$featured_query = new WP_Query( $args );
if ($featured_query->have_posts()) {
while ($featured_query->have_posts()) :
$featured_query->the_post();
$product = get_product( $featured_query->post->ID );
echo $product->title; echo "test";
// Product info here
endwhile;
}
wp_reset_query();
The following query returned 20 results. The code was placed in header.php. Using woocommerce 3.x.
First your code is a bit outdated, since Woocommerce 3, as get_product() need to be replaced with wc_get_product() and $product->title; by $product->get_title();…
Once done your code works and you will get 3 featured products:
$meta_query = WC()->query->get_meta_query();
$tax_query = WC()->query->get_tax_query();
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
);
$featured = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 3, // <== <== <== 3 products
'meta_query' => $meta_query,
'tax_query' => $tax_query,
) );
// Get the products count in the query
echo '<p>Featured products count: ' .$featured->post_count . '</p>';
if ($featured->have_posts()) : while ($featured->have_posts()) :
$featured->the_post();
$product = wc_get_product( $featured->post->ID );
echo $product->get_title() . '<br>';
// Product info here
endwhile; endif;
wp_reset_postdata();
It should work for you as I have tested successfully this code on header.php file…
As before Woocommerce 3, the "featured products" where handled by post meta data (a meta query), you may need to update product terms count going to Woocommerce settings > status > tools. In "Term counts" section click on "Recount terms".
You should be using wp_reset_postdata() instead of wp_reset_query() since WP_query doesn't overwrite the main query.
If that doesn't solve your issue, make sure any other custom loops use the appropriate reset, and/or try renaming the variable $featured_query if you're using it elsewhere - it may be inheriting posts from a previous loop.
You could also try adding the 'nopaging' => true and 'ignore_sticky_posts' => true arguments
I hate to suggest it, but if you can't figure out why it's returning 20 posts instead of 2, you could just break your while loop with a counter:
if ($featured_query->have_posts()) {
$counter = 0;
while ($featured_query->have_posts()) : $featured_query->the_post();
/* Do post stuff here */
$counter++;
if( $counter == 2 ) break;
endwhile;
}
I'm currently working a wordpress loop to retrieve blog posts, their titles, featured image, date and category. With that said, I'm attempting to offset the loop begin on the 5th descending post because the previous 4 are referenced earlier on the page.
I have successfully offset the posts but it seems that I can't grab the category.
<?php
$post_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC',
'offset' => 4
);
$post_query = new WP_Query($post_args);
if ($post_query->have_posts() ):
$count = 1;
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => true
) );
while ( $post_query->have_posts() ) : $post_query->the_post();
$feat_img = wp_get_attachment_url( get_post_thumbnail_id() );
?>
<div class="col-sm-3 col-xs-6">
<div class="featured-img" style="background-image: url(<?php echo $feat_img; ?>)"
<?php the_date('F j Y', '<h6>', '</h6>'); ?>
<h3><?php the_title(); ?></h3>
<div class="category"><?php echo $terms->name; ?></div>
</div>
</div>
I tried a slightly different approach and was able to get each posts category using a foreach loop, followed by a while and if loop. While I successfully got each posts category, the offset wasn't cooperating. Perhaps I'm overthinking it. Here's my other attempt at this.
<?php
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => true,
) );
$count = 1;
foreach ( $terms as $term ) :
$post_args = array(
'offset' => 4,
'post_type' => 'post',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $term->slug
)
),
);
$post_query = null;
$post_query = new WP_Query($post_args);
if ( $post_query->have_posts() ) :
while ($post_query->have_posts() ) : $post_query->the_post();
$feat_img = wp_get_attachment_url( get_post_thumbnail_id() );
?>
Anyone mind lending a hand to help accomplish both tasks? Any input would be greatly appreciated. Thank you in advance.
you need to set "posts_per_page" to some other value than -1, it's explained well in documentation
https://codex.wordpress.org/Class_Reference/WP_Query
posts_per_page (int) - number of post to show per page (available
since Version 2.1, replaced showposts parameter). Use
'posts_per_page'=>-1 to show all posts (the 'offset' parameter is
ignored with a -1 value). Set the 'paged' parameter if pagination is
off after using this parameter. Note: if the query is in a feed,
wordpress overwrites this parameter with the stored 'posts_per_rss'
option. To reimpose the limit, try using the 'post_limits' filter, or
filter 'pre_option_posts_per_rss' and return -1
Hi I have created my own custom post type within Wordpress to contain projects that i can call via my theme files.
I am new to creating my own themes. I currently am using the following code in my single.php file to call in related articles based on the category of the blog post.
<?php
// Default arguments
$args = array(
'posts_per_page' => 3, // How many items to display
'post__not_in' => array( get_the_ID() ), // Exclude current post
'no_found_rows' => true, // We don't ned pagination so this speeds up the query
);
// Check for current post category and add tax_query to the query arguments
$cats = wp_get_post_terms( get_the_ID(), 'category' );
$cats_ids = array();
foreach( $cats as $wpex_related_cat ) {
$cats_ids[] = $wpex_related_cat->term_id;
}
if ( ! empty( $cats_ids ) ) {
$args['category__in'] = $cats_ids;
}
// Query posts
$wpex_query = new wp_query( $args );
// Loop through posts
foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<div class="col-md-4 related-post">
<?php the_post_thumbnail('large'); ?>
<?php the_title(); ?>
</div>
<?php
// End loop
endforeach;
// Reset post data
wp_reset_postdata(); ?>
In my new post type "projects" i would like to call in related projects. Which im assuming would be very similar code except i need to stop it looking for posts and instead look for my projects.
Here is my code for new post type:
// Projects
add_action( 'init', 'create_post_type' );
add_post_type_support( 'bw_projects', 'thumbnail' );
add_post_type_support( 'bw_projects', 'custom-fields' );
function create_post_type() {
register_post_type( 'bw_projects',
array(
'labels' => array(
'name' => __( 'Projects' ),
'singular_name' => __( 'Projects' )
),
'public' => true,
'has_archive' => true,
'taxonomies' => array( 'category','post_tag'),
)
);
}
What would i need to change in my first code snippet in order to look for bw_projects and not look for 'posts' anymore. I tried playing around and changing certain lines myself but i caused more issues and stopped the page loading. Is this even right i can use the same code, slightly altered or would i need something completely different?
Thanks in advance.
You can get any post type that you require using get_posts();
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'projects',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
Simply set the 'post_type' argument to that of you custom post type, to get only these posts. You can also set the number of post, and filter by category etc.
You can find more info in the codex.
Alternatively, if you wanted to keep something similar to your existing code you could try using 'pre_get_posts' to filter the query to just your projects. However you'd need to remember to add / remove this filter so it only operates on the queries that need it.
To display the posts you can use a simple foreach to churn them out. You#d obviously want to do some sort of styling to get the layout correct:
$args = array("posts_per_page" => 10, "orderby" => "comment_count");
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
echo "<h1>" . $post->post_title . "</h1><br>";
echo "<p>" . $post->post_content . "</p><br>";
}
Or a really concise way of doing all of the above would be something like:
$args = array("posts_per_page" => 5, "post_type" => "projects");
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
echo "<h1>" . $post->post_title . "</h1><br>";
echo "<p>" . $post->post_content . "</p><br>";
}
I´m trying to get the post thumbnail and another attached image from the last post of a custom post type (called parceiros-e-links).
What I got is to get all posts with images and show them... but I need to show only the last post and show it with two images (the_post_thumbnail and the wp_get_attachment_image)
Here is my current code:
<?php
$query = new WP_Query( array( 'post_type' => 'parceiros-e-links', 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'DESC' ) ); //the first loop where I filter the posts from custom post type and by date
if( $query->have_posts() ){
while($query->have_posts()){
$query->the_post();
$image_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => 2, 'post_parent' => get_the_ID() ) ); //the second loop where I filter the posts with attachments and limit to two
while( $image_query->have_posts() ) { $image_query->the_post();
//code below prints the two attachments: thumbnail and another one.
if(has_post_thumbnail()){
the_post_thumbnail('home-parceiros');
}
echo wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' );
}
}
}
?>
I already spent many hours searching similar situations and trying to filter this code but I'm without ideas... Because if I limit the query's first posts_per_page to 1, if the most recent post doesn't has an attachment, no images will be printed!
Any clues about how to limit the number of posts with attachments inside a custom post type?
Thanks!
Your mistakes was at the_post_thumbnail('home-parceiros'); and echo wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' ); Read about the correct attributes given for this two functions here:
http://codex.wordpress.org/Function_Reference/the_post_thumbnail
http://codex.wordpress.org/Function_Reference/wp_get_attachment_image
Here is my suggestion:
$query = new WP_Query(
array(
'post_type' => 'parceiros-e-links',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC'
)
);
if($query->have_posts()) :
while($query->have_posts()) : $query->the_post();
if(has_post_thumbnail()) : the_post_thumbnail(); endif;
$args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo wp_get_attachment_image( $attachment->ID, 'full' );
}
}
endwhile;
endif;
Please let me know :)
Example #2 updated:
$query = new WP_Query( array( 'post_type' => 'parceiros-e-links', 'posts_per_page' => 1, 'orderby' => 'date', 'order' => 'DESC' ) ); //the first loop where I filter the posts from custom post type and by date
if( $query->have_posts() ){
while($query->have_posts()){
$query->the_post();
$image_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => 2, 'post_parent' => get_the_ID() ) ); //the second loop where I filter the posts with attachments and limit to two
while( $image_query->have_posts() ) { $image_query->the_post();
//code below prints the two attachments: thumbnail and another one.
if(has_post_thumbnail()){
the_post_thumbnail('home-parceiros');
}
echo wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' );
}
}
}