After digging around StackOverflow and putting together pieces of code, I managed to formulate this below code. However, I can't seem to figure out why it won't display on the page.
At the present time I just have the word "test" just to see if the loop would fire.
What is the issue?
<?php add_shortcode( 'jobs-search-results', 'jobs_search_results' );
function jobs_search_results() {
ob_start();
$jobs_search_results_args = array (
'post_type' => array( 'job' ),
'meta_query' => array(
array(
'key' => 'client_state',
'value' => 'Alabama',
),
),
);
$jobs_search_results_query = new WP_Query( $jobs_search_results_args );
if ( $jobs_search_results_query->have_posts() ) : while($jobs_search_results_query->have_posts()) : $jobs_search_results_query->the_post();
?>
test
<?php endwhile;
$jobs_search_results_output = ob_get_clean();
return $jobs_search_results_output;
endif;
}
?>
Related
I am trying to feed in a tax_query argument with a variable pulling from a Custom Field. Yet nothing is returned. I did a var_dump on my variable and it keeps returning "boolean:False". So I thought maybe I needed to run a loop (my custom field is a sub_field of a group), and then var_dump returns nothing at all. I'm fairly new to WP/PHP development, not sure what's going on here. Any help would be greatly appreciated!
<?php
if(have_rows('wine_profile')) :
while(have_rows('wine_profile')) : the_row();
$label = get_sub_field("taxonomy_label");
var_dump($label);
$wineProfiles = new WP_Query(array(
'posts_per_page' => '-1',
'post_type' => 'wines',
'tax_query' => array(
array(
'taxonomy' => 'labels',
'field' => 'slug',
'terms' => $label
)
),
'order' => 'DESC',
'orderby' => 'ID'
));
if ($wineProfiles->have_posts()) : while ($wineProfiles->have_posts()) : $wineProfiles->the_post();
get_template_part('includes/component', 'colorBlockLg');
get_template_part('includes/component', 'profile');
?>
<?php endwhile;
endif; endwhile; endif; ?>
Try to use -
$label = get_sub_field("taxonomy_label",get_the_ID());
var_dump($label);
I guess because you haven't started post loop!
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Put your code here //
} // end while
} // end if
?>
``
I'm going to have a WP_Query run with arguments based on user input. The user can select multiple categories/terms and the query will filter based on the AND boolean.
// main arguments
$args = array(
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'industry',
'terms' => $user_input,
),
array(
'taxonomy' => 'format',
'terms' => $user_input2,
),
),
);
// less specific arguments
$lessargs = array(
'tax_query' => array(
array(
'taxonomy' => 'industry',
'terms' => $user_input,
),
),
);
If no results are returned in the first query, I want to run the second query with less specificity ($lessargs). I know I need to use if/else statements but I don't know the correct way to do this within the loop. Example:
<?php $the_query = new WP_Query( $args ); ?>
<?php if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : the_post(); ?>
// Return Query Results
<?php endwhile; ?>
<?php else : ?>
<?php $the_second_query = new WP_Query( $less_args ); ?>
<?php while ($the_second_query->have_posts()) : the_post(); ?>
// Return Second Query Results
<?php endwhile; ?>
<?php endif; ?>
Is this the proper way to conditionally call queries if the previous query returns empty?
I always build my own loop whenever I needed a custom query.
You can probably do it like this,
# First Argument
$args = array(
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'industry',
'terms' => $user_input,
),
array(
'taxonomy' => 'format',
'terms' => $user_input2,
),
),
);
# Second Argument
$lessargs = array(
'tax_query' => array(
array(
'taxonomy' => 'industry',
'terms' => $user_input,
),
),
);
$query = new WP_QUery( $args ); //Run First Query
$posts = $query->get_posts(); //Get Post of first Query
if ( !$posts ) { //If no post on First query Run Second Query
$query = new WP_QUery( $lessargs );
$posts = $query->get_posts(); //Get Post of second query
}
if ( !$posts ) return 'No Result Found'; //stop execution if no results
foreach( $posts as $post ) { //Loop through each result
_e( $post->post_title ); // Echo the title
}
}
I would do it slightly differently, just to cater for the fact that neither query will return any rows:
$the_query = new wp_query($args);
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
the_post();
// Return Query Results
}
} else {
$the_query = new wp_query($lessargs);
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
the_post();
// Return Second Query Results
}
} else {
echo '<p>No posts found.</p>';
}
}
Note you have a typo with your $lessargs variable.
An example of the query_string would be: testdev.com/test-results/?Title=166
I have the following code below. What I can't figure out is how to set up the last array to get the ID and then produce the correct post accordingly. (the relation can be ignored, the other arrays I have are working and weren't needed for the example)
Any tips/help would be appreciated
<?php
**THIS IS WHERE I TRIED TO GET THE ID**
$queryTitle = $_GET['Title'];
$pageTitle = the_title();
$args = array(
// all your args here
'post_type' => 'resorts',
'meta_query' => array(
'relation' => 'OR',
array( **THIS IS WHERE I TRIED TO GET THE TITLE FROM THE URL**
'key' => $pageTitle,
'value' => $queryTitle,
'compare' => '=',
),
)
);
$query = new WP_Query( $args );
if($query->have_posts()) : while ($query->have_posts()): $query->the_post(); ?>
content etc goes here
<?php endwhile; else : ?>
<p>No results found, modify your search criteria and try again!</p>
<?php endif; ?>
get_page() could be used:
$queryTitle = $_GET['Title'];
$your_page = get_page($queryTitle);
echo $your_page->post_title;
I have a specific category-slug.php page.
I want to display the first category "banner-ads" with only 1 post per page and then below that, display 3 posts per page from the category "featured."
I don't want to use:
**query_posts( 'posts_per_page=4' );**
I've tried the pre_get_posts function but can't seem to get it working.
Right now the number of posts per page that's displaying is the number I assigned in Settings->Reading
Here's my current code:
$args1 = array(
'category_name' => 'banner-ads',
'posts_per_page' => 1
);
$the_query = new WP_Query( $args1 );
while ( $the_query->have_posts() ) :
$the_query->the_post();
ar2_render_posts( null, array ( 'type' => 'node' ), true );
endwhile;
wp_reset_postdata();
$args2 = array(
'category_name' => 'featured',
'posts_per_page' => 3
);
$query2 = new WP_Query( $args2 );
while( $query2->have_posts() ):
$query2->next_post();
ar2_render_posts( null, array ( 'type' => 'traditional' ), true );
endwhile;
wp_reset_postdata();
You haven't made reset: wp_reset_postdata();
Useful about categories: http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
Here is mine for one of pages
First loop:
<?php
$postq1 = new WP_Query(
array(
'post_type' => array('post','yourcustom'),
'posts_per_page' => 1,
'category_name'=>'banner-ads')
);
if($postq1->have_posts()):
while ( $postq1->have_posts() ) :
$postq1->the_post();?>
<article id="post-<?php the_ID();?>">....</article>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
Second loop:
<?php
$postq2 = new WP_Query(
array(
'post_type' => array('post','yourcustom'),
'posts_per_page' => 3,
'category_name'=>'featured')
);
if($postq2->have_posts()):
while ( $postq2->have_posts() ) :
$postq2->the_post();?>
<article id="post-<?php the_ID();?>">....</article>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
... rest of code with query_posts();
Okay so I finally figured it out. If you want to force it to take certain number of posts then add the filter with its function then at the end of the loop remove it. I realized for my second loop that the ar2_render_posts() function was conflicting with the code so I'm opting to redoing that function from scratch since its basically a layout function.
add_filter('post_limits', 'my_post_limits');
function my_post_limits( $limit ) {
if ( in_category('banner-ads') ) {
return 'LIMIT 0, 3';
}
return $limit;
}
$args1 = array(
'category_name' => 'banner-ads'
);
$the_query = new WP_Query( $args1 );
while ( $the_query->have_posts() ) :
$the_query->the_post();
ar2_render_posts( null, array ( 'type' => 'node' ), true );
endwhile;
wp_reset_postdata();
remove_filter('post_limits', 'my_post_limits');
it is pretty simple, just alter query_args in ar2_render_posts
ar2_render_posts( null, array (
'query_args' => array ( 'posts_per_page' => 1 ),
), true );
I created a field with the Adv Custom Fields plugin which allows the user to select which section the page is under (like categories). On each page I'd like to display a sidebar which shows a list of pages with the same section. I attempted to use meta_query and I don't get any results. I would also like to display the parent page first if there's a way to do it. Here's my query:
<ul class="test-menu">
<?php
$section = get_field('section');
$args = array(
'meta_query' => array(
array(
'key' => 'section',
'value' => $section
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
Seems like you need to specify a post_type in your query and you are missing the compare bit although I'm not sure which one throw you off :
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'section',
'value' => $section,
'compare' => "="
)
)
);
The post type can probably be an array if you have multiple type of custom posts.