Wordpress query pages with certain template - wordpress

Is there a way to query Wordpress pages that have certain template?
Here's what I got, but doesn't show anything:
<?php $my_query = new WP_Query(array( 'meta_key' => '_wp_page_template', 'meta_value' => 'template-city.php' )); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li>
<?php the_title(); ?>
</li>
<?php endwhile; ?>
Of course, there is page template file named template-city.php

if post_type is left out WP will look for post, and you are looking for pages.
<?php
$args = array(
'post_type' => 'page',//it is a Page right?
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => 'template-city.php', // template name as stored in the dB
)
)
);
$my_query = new WP_Query($args)
?>

Related

WP loop filtering with ACF values

How to filter by ACF fields in WP loop?
<?php
$company = the_field('company');
$loop = new WP_Query( array(
'post_type' => 'jobs',
'posts_per_page' => '100',
'meta_key' => 'company',
'meta_value' => the_field('company')
)
);
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php get_template_part( 'components/loop' );?>
<?php endwhile; wp_reset_query(); ?>
If the meta_value is a string it works. Wondering how to make this work dynamically.
You need to use get_field instead of the_field. You can print custom-filed values with the get_field function.
$loop = new WP_Query(
array(
'post_type' => 'jobs',
'posts_per_page' => '100',
'meta_key' => 'company',
'meta_value' => get_field('company'),
)
);
?>
<?php while ($loop->have_posts()): $loop->the_post();?>
<?php get_template_part('components/loop');?>
<?php endwhile;
wp_reset_query();?>

Wordpress display custom post type with custom taxonomy and current term

Difficult this one:
I'm trying to all posts from a custom post type 'specialisaties' with the custom taxonomy 'specialismen' and need to load the current loaded 'term' from the URL.
Currently I have this code that outputs the term 'superpower' .
<?php $loop = new WP_Query(array('post_type'=>'specialisaties', 'taxonomy'=>'specialismen', 'term' => 'superpower', 'orderby' => 'menu_order', 'order' => 'asc')); ?>
<?php while ($loop->have_posts()) : $loop->the_post(); ?>
<h1><?php the_title() ?></h1>
<?php the_content() ?>
<?php endwhile; ?>
This loads the specific post with the term 'superpower'. How do I fetch the 'term' dynamically from the URL I'm loading?
Thanks in advance,
Fixed it with get_term_by .
<?php $term = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') ); ?>
<?php $loop = new WP_Query(array('post_type'=>'specialisaties', 'taxonomy'=>'specialismen', 'term' => $term->name, 'orderby' => 'menu_order', 'order' => 'asc')); ?>

How to create a bxslider with wordpress post type categories

I have a taxonomy called portfolio_cat with its categories.So now i need to create a slider with that categories as a title and their post items.How i can do that? What Wordpress loop i need to have so i could put in a slider Wordpress categories with their posts?
I dont know how to customize this loop to fit in
<?php
$query = new WP_Query( array('post_type' => 'portfolio', 'posts_per_page' => 7, 'order' => ASC ) );
while ( $query->have_posts() ) : $query->the_post();
?>
I think you need this:
$portfolioArgs = array(
'order' => 'ASC',
'post_type' => 'portfolio',
'posts_per_page' => 7,
'tax_query' => array(
array(
'taxonomy' => 'portfolio_cat',
'field' => 'slug'
)
)
);
$query = new WP_Query( $portfolioArgs );
while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="slide">
<?php the_title( '<h2>', '</h2>' ); ?>
<?php the_excerpt(); ?>
</div>
<?php
endwhile;
?>

Wordpress, querying pages by category and displaying meta info

I have a wordpress blog that has a theme with some custom code to access posts by category. I've not used wordpress until now, and would like to make this code work for pages instead of posts. So far I've tried a few basic queries from codex.wordpress.org without much success.
Can anyone assist with this please?
<?php $some_array_of_pages = new WP_Query( array( 'ignore_sticky_posts' => 1, 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'category_name' => 'category' ) ); ?>
<?php while ($some_array_of_pages->have_posts()) : ?>
<?php $properties->the_post(); ?>
<?php $meta1 = get_post_meta( get_the_ID(), 'meta_name', true ); ?>
<?php $meta2 = get_post_meta( get_the_ID(), 'meta_name', true ); ?>
<?php if ($meta1 && $meta2): ?>
//do something here
<?php endif; ?>
<?php wp_reset_postdata(); ?>
<?php endwhile; ?>
Add 'post_type' to your query:
<?php $some_array_of_pages = new WP_Query( array( 'post_type'=>'page' ) ); ?>
http://codex.wordpress.org/Class_Reference/WP_Query
Full example from OP:
<?php $some_array_of_pages = new WP_Query( array( 'ignore_sticky_posts' => 1, 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'category_name' => 'category', 'post_type' => 'page' ) ); ?>

Wordpress - How to list most commented posts in certain categories

I am looking to add a top ten type list to my WP website.
I currently have the following but I need to be able to make it get posts from multiple category IDs, does anybody know how I would go about doing this?
Thanks in advance for your help.
<div>
<?php
$args=array(
'cat' => 75, // this is category ID
'orderby' => 'comment_count',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 10, // how much post you want to display
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) { ?>
<ul>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php }
wp_reset_query(); ?>
</div>
I've tested your code on a dev site of mine and it does what you are wanting; however, with WP_DEBUG set to true, I get an error indicating that the parameter caller_get_posts is deprecated in 3.1. Depending on your PHP setup and server config, this could cause problems for you. I would suggest making the following change:
<div>
<?php
$args=array(
'cat' => 75, // this is category ID
'orderby' => 'comment_count',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 10, // how much post you want to display
'ignore_sticky_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) { ?>
<ul>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php }
wp_reset_query(); ?>
</div>
With the only change being substituting ignore_sticky_posts for caller_get_posts.

Resources