Adding multiple options to PHP query - php

I have a property listing site and I need help with the code that filters search results. The code below includes all properties with the contract type "southern-oregon". I would like it to ALSO include contract types = to "medford-office". Can someone tell me how to add that to the code?
Here is the code:
<?php
echo $div; $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array('post_type'=>'property','post_status'=>'publish','paged'=>$paged,
'author'=>$agent, 'property-contract-type'=>'southern-oregon'));
if(have_posts()):while(have_posts()):the_post();
$property_images = get_post_meta(get_the_ID(),'imic_property_sights',false);
$total_images = count($property_images);
$property_term_type = '';
$property_area = get_post_meta(get_the_ID(),'imic_property_area',true);
$property_baths = get_post_meta(get_the_ID(),'imic_property_baths',true);
$property_beds = get_post_meta(get_the_ID(),'imic_property_beds',true);
$property_parking = get_post_meta(get_the_ID(),'imic_property_parking',true);
$property_address = get_post_meta(get_the_ID(),'imic_property_site_address',true);
$property_city = get_post_meta(get_the_ID(),'imic_property_site_city',true);
$property_price = get_post_meta(get_the_ID(),'imic_property_price',true);
$contract = wp_get_object_terms( get_the_ID(), 'property-contract-type',
array('fields'=>'ids'));
$property_id = get_post_meta(get_the_ID(),'imic_property_site_id',true);
$property_area_location = wp_get_object_terms(get_the_ID(), 'city-type');
$sl = '';
$total_area_location = count($property_area_location);
$num = 1;
foreach($property_area_location as $sa) {
$conc = ($num!=$total_area_location)?'->':'';
$sl .= $sa->name.$conc; $num++;
}
// We get Longitude & Latitude By Property Address
$property_longitude_and_latitude=get_post_meta(get_the_ID(),'imic_lat_long',true);
if(!empty($property_longitude_and_latitude)){
$property_longitude_and_latitude = explode(',',
$property_longitude_and_latitude);
}else{
$property_longitude_and_latitude=getLongitudeLatitudeByAddress($property_address);
}
global $imic_options;
$currency_symbol = imic_get_currency_symbol($imic_options['currency-select']);
$src = wp_get_attachment_image_src(get_post_thumbnail_id(),'150-100-size');
if(!empty($src)):
$image_container= '<span class ="property_image_map">'.$src[0].'</span>';
else:
$image_container='';
endif;
if(!empty($contract)) {
$term = get_term( $contract[0], 'property-contract-type'); $property_term_type = $term->name;
}
if($design_type=='listing') {
?>

I expect this is the core of your filtering system (significantly reformatted for readability - ask your developer to fix this please):
query_posts(
array(
'post_type' => 'property',
'post_status' => 'publish',
'paged' => $paged,
'author' => $agent,
'property-contract-type' => 'southern-oregon'
)
);
At a guess, try replacing 'southern-oregon' with an array, thus:
['southern-oregon', 'medford-office', ]
I would guess this would do an IN, which is essentially an OR.

Related

Wordpress get result from title (anywhere), not just first word

Currently this is working code that is queried as standalone search.php.
I have an issue that it searches beginning of title and not anywhere in title what I exactly need.
I have a feeling that I'm close to solution and I tried couple of things and it is probably something that I'm missing in post $args..
global $wp_query;
$type = 'qa';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'showposts'=>100,
'orderby'=> 'menu_order',
'order' => 'DESC'
);
$my_query = null;
$my_query = new WP_Query($args);
$data = array();
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$title = get_the_title();
$url = get_the_permalink();
$data[$title] = $url;
endwhile;
}
if(isset($_POST['latestQuery'])){
$latestQuery = $_POST['latestQuery'];
$latestQueryLength = strlen($latestQuery);
$result = array();
foreach($data as $name => $url){
if (substr(strtolower($name),0,$latestQueryLength) == strtolower($latestQuery)){
$result[$name] = $url;
}
}
echo json_encode($result);
}
This is already working in local environment.
JavaScript queries search.php file (above) where it gets json array returned based on query... for example "cla.."
{"Classic something":"http:\/\/www.something\/qa2","Classic something else":"http:\/\/www.something\/qa"}
So I have properly retuned two items that only have "Classic" mentioned in post title.
If I type "som" or "els" it doesn't return anything and it should match as well all "some" and "else" items.
I think it is not an issue with javascript code but with PHP code.
Thanks!
Solved if would be helpful to someone in future.
if(isset($_POST['latestQuery'])){
$latestQuery = $_POST['latestQuery'];
global $wp_query;
$type = 'qa';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
's' => "$latestQuery", // defined query
'showposts'=>100,
'orderby'=> 'menu_order',
'order' => 'DESC',
);
$my_query = null;
$my_query = new WP_Query($args);
$data = array();
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$title = get_the_title();
$url = get_the_permalink();
$data[$title] = $url;
endwhile;
}
$result = array();
foreach($data as $name => $url){ // removed if
$result[$name] = $url;
}
echo json_encode($result);
}

Wordpress get_posts() fetch next batch of posts

I'm using the get_posts() function to fetch a batch of posts from a custom post type, sorted by ID, modify those posts and then fetch the next batch.
I have the following code:
<?php
require_once('wp-load.php');
$temp_list_of_products_array = get_posts( array('post_type' => 'sale', 'numberposts' => 10 ) );
$temp_list_of_products_array_length = count( $temp_list_of_products_array );
for ($xt = 0; $xt < $temp_list_of_products_array_length; $xt++) {
$temp_product_id = $temp_list_of_products_array[$xt]->ID;
$temp_product_untranslated_field = get_post_meta($temp_product_id, 'wpcf-product-details', true);
$temp_product_translated_field = get_post_meta($temp_product_id, 'wpcf-translated-product-details', true);
$temp_product_description_language = 'en';
if ($temp_product_translated_field == null) {
$temp_product_translated_contents = google_translate_text($temp_product_untranslated_field, $temp_product_description_language);
update_post_meta($temp_product_id, 'wpcf-translated-product-details', $temp_product_translated_contents);
}
echo $temp_product_id;
}
?>
This works great but the problem is that it only loads the first 10 posts ordered by date.
My question is, how do I get the next batch of 10 posts without have a user activated pagination call?
Thanks
first get the current page
$paged=($query_vars['paged']!=0 ? $query_vars['paged'] : 1);
now calculate the offset value
$numberposts=10;
$ofdset=$numberposts* ($paged - 1) ;
add your code
$temp_list_of_products_array = get_posts( array('post_type' => 'sale', 'numberposts' => 10 ,'offset'=>$offset) );
$temp_list_of_products_array_length = count( $temp_list_of_products_array );
for ($xt = 0; $xt < $temp_list_of_products_array_length; $xt++) {
$temp_product_id = $temp_list_of_products_array[$xt]->ID;
$temp_product_untranslated_field = get_post_meta($temp_product_id, 'wpcf-product-details', true);
$temp_product_translated_field = get_post_meta($temp_product_id, 'wpcf-translated-product-details', true);
$temp_product_description_language = 'en';
if ($temp_product_translated_field == null) {
$temp_product_translated_contents = google_translate_text($temp_product_untranslated_field, $temp_product_description_language);
update_post_meta($temp_product_id, 'wpcf-translated-product-details', $temp_product_translated_contents);
}
echo $temp_product_id;
}
add pagination code at bottom of loop
check this for wordpress pagination with get_posts function
https://wordpress.stackexchange.com/questions/137100/using-pagination-with-get-posts-on-page-type
You can simply use the paged parameter:
$current_page = 1; // <-- Modify this to your needs!
$temp_list_of_products_array = get_posts(
array(
'paged' => $current_page,
'post_type' => 'sale',
'posts_per_page' => 10
)
);
for paging.

Wordpress Pagination - max_num_pages = 0

Wordpress ninjas... I require some help regarding pagination.
I have the following code :
global $wp_query;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array_merge( $wp_query->query_vars,
array(
'post_type' => 'attachment',
'posts_per_page' => 10,
'paged' => $paged
)
);
How come $query->max_num_pages returns 0 ?
And is there a way to alter the original archive query with the $query->max_num_pages ?
[EDIT]
In Addition to the above code I have the following
$output = '<div class="download-attachments left_content_container"><ul>';
//this does not retrieve anything
$query = new WP_Query( $args );
// this does retrieve posts
$attachments = get_posts( $args );
// this block is only used for testing purposes..
// ie. it is not being executed because the query does not return anything, Whilst get_posts() works
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
echo the_ID();
}
}
foreach ($attachments as $attachment) {
setup_postdata($attachment);
//get attachments metadata etc.
$url = wp_get_attachment_url( $attachment->ID );
$title = get_the_title( $attachment );
$caption = $attachment->post_excerpt;
$size = get_readable_size( filesize(get_attached_file( $attachment->ID )) );
$icon = get_icon_for_attachment( $attachment->ID );
$mime = get_post_mime_type( $attachment->ID );
$date_added = $attachment->post_date;
$filename = ( !$caption == '' ) ? $caption : $title ;
$description = ($attachment->post_content != '') ? '<span class="attachment-author"><span class="attachemnt-label">Added by: ' . $attachment->post_content . '</span></span>' : '';
$output .= '<li class="'.$mime.'">
<img class="attachment-icon" src="'.$icon.'" alt="pdf">
'. $filename .' '.$description.'
<span class="attachment-date"><span class="attachment-label">Date added: </span>'.$date_added.'</span>
<span class="attachment-size"><span class="attachment-label">Attachment size: </span>'.$size.'</span></li>' ;
}
$output .= '</ul></div>';
echo $output;
echo '<div class="paging pull-left">';
$paged = $wp_query->get( 'paged' );
if ( ! $paged) {/* do nothing */ ;}
else { bootstrap_pagination(); var_dump($wp_query->max_num_pages); // returns 0 }
echo '</div>';
You have a couple of problems here. You don't need to call the global $wp_query because you are not going to use it. Secondly, array_merge is totally out of place. This should not be used at all in this situation. Your code should look something like this
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'attachment',
'posts_per_page' => 10,
'paged' => $paged
)
);
$query = new WP_Query( $args );
For more info on custom queries with WP_Query have a look here

Insert a post type every x posts in custom loop

I was hoping someone could help me with this problem.
I'm trying to make a loop in wordpress with two different post types ('product' and 'outfits')
The code below is working fine, this outputs a list of the two post types ordered by newest to older.
$loop = new WP_Query( array(
'post_type' => array( 'product', 'outfits' ),
'posts_per_page' => 15
) );
$counter = 0;
?>
<?php if ( $loop->have_posts() ) { while ( $loop->have_posts() ) { $loop->the_post();
$counter++; ?>
<?php $loop->is_home = false; ?>
<?php
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
$titulo = get_the_title();
$content = apply_filters ('the_content', $post->post_content); ?>
<div class="caja-<?php echo $counter; ?>">
<?php if ( $post->post_type == "product" ) {
//some stuff
<?php } else {
//some stuff
} ?>
</div>
<?php } } wp_reset_postdata();
What I would like to do is to insert a product post type after X number of outfits.
I was trying to merge a similar solution I've found in other question with no luck. I'm not a php expert so any help is appreciated
<?php
$args = array('post_type'=>'post', 'posts_per_page'=>9, 'category_name'=>'news');
$posts = get_posts($args);
$args = array('post_type'=>'testimonials', 'posts_per_page'=>3);
$testimonials = get_posts($args);
// see how many of the regular posts you got back //
$post_count = count($posts);
// see how many testimonials you got back //
$testimonial_count = count($testimonials);
// add them up to get the total result count //
$total_count = $post_count + $testimonial_count;
// Loop through the total number of results //
for($i = 1; $i <= $total_count; $i++){
// assuming you want to show one testimonial every third post //
if($i % 3 == 0){
// this means you're on the a third post, show a testimonial //
setup_postdata($testimonials[$i]);
}
else{
/** show a regular post */
setup_postdata($posts[$i]);
}
/** and now handle the output */
?><h1><?php the_title();?></h1><?php
} ?>
$x = 3;
$products = get_posts(array(
'post_type' => 'product',
'posts_per_page' => -1
));
$outfits = get_posts(array(
'post_type' => 'outfit',
'posts_per_page' => -1
));
foreach ($outfits as $num => $outfit) {
if ( ($num+1) % $x == 0) {
if (isset($products[($num+1)/$x - 1])) {
array_splice( $outfits, $num, 0, array($products[($num+1)/$x - 1]) );
}
}
}
foreach ($outfits as $outfit) {
$posts_id[] = $outfit->ID;
}
$query = new wp_query(array(
'post_type' => array('outfit', 'product'),
'post__in' => $posts_id,
'posts_per_page' => 15,
'orderby' => 'post__in'
));

Modify Wordpress plugin to show posts by category instead of id#

I am using this Wordpress plugin: http://wordpress.org/plugins/carousel-of-post-images/
It shows a slider of posts & displays the featured image.
It is currently set to show posts by id which need to be individually entered into the shortcode. Is it possible to show posts by category instead so that I can set the category of the post and see only that category in the slider?
Here is all the code that I believe is responsible.
function copi_carousel_get_images($size = 'medium' , $orderby, $posts, $count, $class = ''){
global $post;
$att_array = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => $orderby,
'numberposts' => $count,
);
$postlist = explode(",",$posts);
$sizes = explode(",", $size);
$html = '';
if (count($sizes) == 2)
{
$width = $sizes[0];
$height = $sizes[1];
$size=$sizes;
}
else
{
$width = get_option($size.'_size_w');
$height = get_option($size.'_size_h');
}
foreach($postlist as $postid)
{
if ($postid != '')
$att_array['post_parent'] = $postid;
$attachments = get_posts($att_array);
if (is_array($attachments)){
foreach($attachments as $att){
$image_src_array = wp_get_attachment_image_src($att->ID, $size);
$url = $image_src_array[0];
if ($url != "")
{
$prefix = '<li><a href="'.get_permalink($att->post_parent).'">';
$suffix = "</a></li>";
$caption = $att->post_excerpt;
if(function_exists('thumbGen'))
$url = thumbGen($url, $width, $height, 'return=1');
$image_html = '<img src="%s" height="'.$height.'" alt="%s">';
$html .= $prefix.sprintf($image_html,$url,$caption,$class).$suffix;
}
}
}
}
return $html;
}
function show_wp_copi_carousel($atts){
$skin = 'tango';
$div='post-carousel';
$imagesize = 'medium';
$orderby = 'rand';
$postid = '';
$count='10';
if (isset($atts['skin']))
$skin = $atts['skin'];
if (isset($atts['imagesize']))
$imagesize = $atts['imagesize'];
if (isset($atts['orderby']))
$orderby = $atts['orderby'];
if (isset($atts['postid']))
$postid = $atts['postid'];
if (isset($atts['count']))
{
$count = $atts['count'];
I do this with Coda slider.
First include the script in the head:
<script src="js/jquery.coda-slider-3.0.js"></script>
<script>
$(function(){
/* Here is the slider using default settings */
$('#slider-id').codaSlider({
autoSlide:true,
autoHeight:false
});
});
</script>
Then I used the loop:
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<div class="coda-slider" id="slider-id">
<?php
$args = array('category' => 15, 'numberposts' => 4, 'order'=> 'ASC');
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<img src="<?php echo $image[0]; ?>"/>
<?php endforeach; ?>
</div>
This should work!
Note: To show post information, just include "the_post" in the loop or whatever content you want to display, the slider script will take care of the rotation.
Also, this needs to be used in a template.

Categories