I have a template named "Foo" in "foo.php", I would like to be able to select all pages that are using that template. I have searched for awhile now but have not been able to find a successful way to do this... Can somebody enlighten me on the proper/only way to do this?
You can get this by using following code
$query = new WP_Query( array( 'meta_key' => '_wp_page_template', 'meta_value' => 'foo.php' ) );
if ( have_posts() ) while ( have_posts() ) : the_post();
<?php the_title(); ?>
<?php endwhile; // end of the loop. ?>
Robot's answer is good, but I thought I'd clarify a few things.
First, You should use the variable for the query you created, so it would be $query->have_posts() etc.
Second, you should specify post_type. I used any, so that it will pull any post types except for revisions.
Last, if this is in a page with any other WP loops, you may want to use wp_reset_query. I added one below and one above just in case, but you only really need this if you have another loop above or below. Remove it if you don't.
Here is the code:
wp_reset_query();
$query = new WP_Query( array(
'post_type' => 'any',
'meta_key' => '_wp_page_template',
'meta_value' => 'foo.php'
) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post(); // WP loop
the_title();
endwhile; // end of the loop.
} else { // in case there are no pages with this template
echo 'No Pages with this template';
}
wp_reset_query();
Hope that helps someone!! Happy coding!
This also works
$pages = get_pages(
array(
'meta_key' => '_wp_page_template',
'meta_value' => 'template.php'
)
);
foreach($pages as $page){
echo $page->post_title.'<br />';
}
http://jorgepedret.com/old/web-development/get-pages-by-template-name-in-wordpress/
Related
The code shown below is not working, when I search, however if I remove the post_type the below works fine for default post.
weblinks is the custom post type for the taxonomy weblinks_guidelines
If any thing is searched:
<?php if(!empty($_POST['search'])) {
$search_term = $_POST['search'];
} else {
$search_term ='';
}
Arguments for the query
$args = array(
'post_type' => 'weblinks',
'posts_per_page' => -1,
's'=>$search_term
);
$my_query = new WP_Query ($args);
if( $my_query->have_posts() ) :
while ($my_query->have_posts()) : $my_query->the_post();
endwhile;
endif;
?>
I'm assuming the sentences are added for our benefit, if not they're not properly commented out. I'd also consider cleaning up your code, the inconsistent indentation will be a pain to deal with later.
That said, your issue is that you're always searching for something, considering the s key is always included in the $args array, you need to add that whole argument conditionally, not just its value. Right now it will be either "search for nothing" or "search for the posted 'search' value. We can fix this by pushing the ['s'] argument into the $args array using the basic syntax of $array['key'] = $value;.
$args = array(
'post_type' => 'weblinks',
'posts_per_page' => -1
);
if( !empty( $_POST['search'] ) ){
$args['s'] = $_POST['search'];
}
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) :
while( $my_query->have_posts() ) : $my_query->the_post();
// Post Loop Code Here
endwhile;
endif;
I've been trying to formulate a way to query by a user role type in wordpress and have been using the following code which I found on Stack Overflow but it isn't working for me:
<?php
$friends = get_users( array( 'role' => 'mns' ) );
$friend_ids = array();
foreach( $friends as $friend )
$friend_ids[] = $friend->ID;
$news = new WP_Query( array( 'author' => implode( ',', $friend_ids ), 'post_type' => 'roleplays', ) );
?>
<?php if ( $news->have_posts() ) : while ( $news->have_posts() ) : $news->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif; ?>
The loop collects all the posts in the custom post type, but doesn't show only posts from the specific role type.
Any ideas?
Thanks in advance.
Josh
Do you included "wp_reset_postdata();"? When using the_post(), wp_reset_postdata() should be use to ensure that you don't get unexpected outcome. Outside that, I don't see what is causing the problem. Is there other custom php codes or plugins that may be interfering?
I need PHP-code for WordPress (Woocommerce) to display random product link.
For example, I have Product1 and want to display on this page (in description of my product):
"See also other products: [Product2 - link] and [Product3 - link]"
Don't know how, I just need php code to insert it in post/pages/products and everywhere I want on my site.
I'm not a coder and I found this code, for example, to display page title with link, but it's not what I need
<?php
echo ''.get_the_title($product_id).'';
?>
But how to get random product, don't know, thanks for help.
Try this :
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'post_type' => 'product' );
$random_products = get_posts( $args );
foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
<?php the_title(); ?>
<?php endforeach;
wp_reset_postdata();
The Perfect solution for outputting a single random product which can be achieved using the following code.
<?php
$post_array=array();
$args = array(
'post_type' => 'product',
'posts_per_page' => 12
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
array_push($post_array,get_the_ID());
endwhile;
$random_key = array_rand($post_array, 1);
echo ''.get_the_title($post_array[$random_key]).'';
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
Have tested the same for you. It worked. Lemme Know if the same worked for you too.
I am trying to modify my tag.php inside Wordpress. Basically, I have my general loop set to the default five posts per page. When users click on a tag from my tag cloud, I'd like it to display all of the relevant results by title. Here is what I have in tag.php:
<p>Tag: <?php single_tag_title(); ?></p>
<?php if (have_posts()) : while (have_posts ()) : the_post (); ?>
<p><?php the_title(); ?></p>
<?php endwhile; endif; ?>
This works perfectly, however it only returns the default amount of five. When I try to add a wp_query using ('posts_per_page' => 1000) before the loop, it returns all of my sites posts, rather than just for the appropriate tag. How can add more results? Thanks!
Thank you very much for the reply SilverKenn, I appreciate it. I was able to sort it out using this instead.
<?php $args = array( 'post_type' => 'post', 'posts_per_page' => -1, 'tag'=> get_query_var('tag') );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<p><?php the_title(); ?></p>
instead of editing a php file whenever you try to customize something via wordpress, you can modify almost everything using functions.php depending on how you code the theme,
Check out post_limits filter http://codex.wordpress.org/Plugin_API/Filter_Reference/post_limits
or pre_get_posts http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
you can limit the results using the above filters,
e.g. pre_get_posts
function cmk_custom_result( $wp_query ) {
$post_type = $wp_query->query['post_type'];
if ( $post_type == 'your-post-type' && is_tag() ) {
$wp_query->set('posts_per_page', '25');
}
}
add_filter('pre_get_posts', 'cmk_custom_result');
post_limist
function cmk_post_result_limits( $limit, $query ) {
if ( !is_admin() && $query->is_main_query() && is_tag() ) {
return 'LIMIT 0, 25';
}
return $limit;
}
add_filter( 'post_limits', 'cmk_post_result_limits', 10, 2 );
Use get_query_var to get the correct tag, like: 'tag'=> get_query_var('tag') then use -1 in your posts array for 'posts_per_page' to get an unlimited number of posts for that tag, otherwise change the number to whatever you want to limit the output.
<?php $args = array( 'post_type' => 'post', 'posts_per_page' => -1, 'tag'=> get_query_var('tag') );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; ?>
I am adding post thumbnail jquery slider to header part but it's resulting weird issue. It is somehow not ending while or stop query and so if I will go to single post or page it is keep displaying loop instead of page or post content.
I have tried two different query but none of them stopping to this weird issue.
First Tried
<?php
query_posts( 'post_status=publish&orderby=rand' );
while (have_posts()) : the_post();
$title_attr = array(
'title' => get_the_title(),
'alt' => get_the_title(),
'class' => get_the_title(),
);
echo '<a href="#post-'.get_the_ID().'" class="scroll theme">';
the_post_thumbnail('thumbnail',$title_attr);
echo '</a>';
endwhile; ?>
Than Second Tried
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('post_status=publish&orderby=rand');
// The Loop
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
$title_attr = array(
'title' => get_the_title(),
'alt' => get_the_title(),
'class' => get_the_title(),
);
echo '<a href="#post-'.get_the_ID().'" class="scroll theme">';
the_post_thumbnail('thumbnail',$title_attr);
echo '</a>';
endwhile; endif; wp_reset_query();?>
None of these stopping to display loop ( all post like index page) in to single post or page.
I will go to single post or page it is keep displaying loop instead of page or post content...
That is because you are not passing it any parameters that would limit the query for single posts. Your query ( $wp_query->query('post_status=publish&orderby=rand'); ) pulls all posts, all the time, and in random order. For single post display you need to pass it a post or page parameter. You probably need to use get_query_var() to check for 'p', 'page_id', or both. Something like this:
$pid = get_query_var('p');
if (!empty($pid)) {
$qry = 'p='.$pid;
} else {
$qry = 'post_status=publish&orderby=rand';
}
$wp_query->query($qry);
There are other possible solutions as well, like is_single().
Also, WordPress uses the variable $wp_query so you should really pick another one instead of clobbering that one.
I found the solution :)
I have added if (have_posts()) before while and end loop with wp_reset_query() and all good now. :)
So here is final code if anyone having same problem..
<?php
query_posts( 'post_status=publish&orderby=rand' );
if ( have_posts()): while (have_posts()) : the_post();
$title_attr = array(
'title' => get_the_title(),
'alt' => get_the_title(),
'class' => get_the_title(),
);
echo '<a href="#post-'.get_the_ID().'" class="scroll theme">';
the_post_thumbnail('thumbnail',$title_attr);
echo '</a>';
endwhile; endif; wp_reset_query(); ?>