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;
Related
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;
}
?>
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.
I've been running in to a problem.
I'm editing a custom wordpress theme. I want to make some adjusments to the FAQ page (created with custom post type).
Right now every subject of question (= also category) is showing only 5 answers (these are posts).
I wanted to know, how I could increate this number, so instead of 5, show 10 or rather, show all answers per subject.
This is the code:
archive-faq.php
<?php $terms = get_terms( 'faq_category' ); ?>
<?php foreach( $terms as $term ):
$args = array (
'post_type' => 'faq',
'tax_query' => array(
array(
'taxonomy' => 'faq_category',
'field' => 'id',
'terms' => $term->term_id,
),
),
);
$query = new WP_Query( $args ); ?>
<h1><?php echo $term->name; ?></h1>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="toggle-holder">
<h5 class="toggle"><span><?php the_title(); ?></span></h5>
<div class="toggle-box">
<div>
<?php the_content(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
An important part is:
<div class="toggle-box">
<div>
<?php the_content(); ?>
</div>
</div>
Where the_content() , is showing the posts that are in a certain category.
However it's nowhere to be found why , the page is only showing up to 5 posts (answers) and not any more ?
Things I've tried:
$query = new WP_Query( $args );
$query_posts('post_per_page=3'); ?>
Also:
putting 'showposts' => 8 under 'terms' => $term->term_id,
And I also tried:
$query = new WP_Query( $args ); ?>
<?PHP
query_posts( array(
'workcap' => $all_post_terms,
'showposts' => 8,
'caller_get_posts' => 1,
'post__not_in' => $do_not_duplicate ) ); ?>
->> In summary:
Why does the page only show up to 5 posts ?
And how do I change this property ?
PS. If you want to see the page: http://goo.gl/UnWRTz
The argument is posts_per_page and not post_per_page. Its a typo?
Try this:
$args = array (
'post_type' => 'faq',
'posts_per_page' => -1,
//for now try without this taxonomy, if it works try with it.
/*'tax_query' => array(
array(
'taxonomy' => 'faq_category',
'field' => 'id',
'terms' => $term->term_id,
),
),*/
);
Btw, you can find more info here: https://codex.wordpress.org/Class_Reference/WP_Query
I have a custom post type set up on my blog and a special page for the taxonomy. On the taxonomy page I am getting the below error. Can anyone give me some tips on how to resolve this error?
The page loads fine and works as I would expect. But I get the below error if I have debug set to true. I would like to resolve this. I pasted the cost from the loop which is run two time on the page with different criteria.
Notice: Trying to get property of non-object in /home3/ans/public_html/wp-includes/post-template.php on line 29
Code:
<?php
query_single('dealers', 'publish', '1', $taxtype, $value);
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php
$address=get_post_meta($post->ID, 'wpcf-street_address', TRUE);
$city=get_post_meta($post->ID, 'wpcf-city', TRUE);
$state=get_post_meta($post->ID, 'wpcf-state_abbreviation', TRUE);
$zip=get_post_meta($post->ID, 'wpcf-zip_code', TRUE);
$phone=get_post_meta($post->ID, 'wpcf-phone_number', TRUE);
$paid=get_post_meta($post->ID, 'wpcf-paid', TRUE);
$post_id=get_the_ID();
get_each_dealer_brand($post_id);?>
<?php
echo "<ul class=\"ullisting\">";
if($paid==1)
{
echo "<li><p class=\"plisting\"><strong>";the_title();echo "</strong></p></li>";
echo "<li><p class=\"plisting\">$address | $city, $state $zip</p></li>";
echo "<li><p class=\"plisting\">P: $phone</p></li>";
echo "<li><p class=\"listing\"><span><small>$brands_list</small></span></p></li>";
}
echo "</ul>";
?>
<?php endwhile; ?>
<?php
wp_reset_query();
wp_reset_postdata();
unset($brands_list);
?>
This is the function referenced above:
function query_single($posttype, $poststatus, $paidvalue, $taxtype, $value) {
global $wp_query;
$wp_query = new WP_Query();
$args = array(
'post_type' => $posttype,
'post_status' => array($poststatus),
'orderby' => 'rand',
'posts_per_page' => 20,
'meta_query' => array(
array(
'key' => 'wpcf-paid',
'value' => array($paidvalue),
'compare' => 'IN',
)
),
'tax_query' => array(
array(
'taxonomy' => $taxtype,
'field' => 'slug',
'terms' => $value
)
)
);
return $wp_query->query($args);
}
This error will arise when you try to access posts inside your theme,
in page-template.php we have,
function get_the_ID() {
return get_post()->ID;
}
Whenever we are accessing posts we need to check the below condition and make sure it works as default, because we may not use wp_reset_postdata(); all the time so,
global $post;
//check if post is object otherwise you're not in singular post
if( !is_object($post) )
return;
//If Object
$somevariable = get_post_meta(get_the_ID(), $something->get_the_id(), TRUE);
Hope this helps. Thanks.
You can try to execute your commands within ACTION (because wordpress is already loaded normally at that time).
like this:
ADD_ACTION('init','your_function');
function your_function(){
YOUR CODES HEREEEEEEEEEEE............
}
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.