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);
}
Related
i have created shortcode to show all testimonial on one page.
i have created custom post type to add each testimonial.
can anybody tell me how to show all testimonial on single page .
this code is used to create sshortcode
below i m mentioning code :-
function fn_testimonials_block()
{
$args = array(
'post_type'=> 'testimonials',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
$pages = $the_query->posts;
//echo "<pre>";
//print_r($pages);
//echo "</pre>";
//exit;
$output = '';
$count = 1;
foreach($pages as $page) {
//-----------------
$author = $page->post_title;
$testimonial = $page->post_content;
$page_url = get_page_link( $page->ID );
$author_image = get_the_post_thumbnail_url( $page->ID, 'thumbnail' );
if ($count%2 == 1)
{
$output .= '<div>';
}
$output .= '<div>
<p>'.$testimonial.'<br>
<img src="'.$author_image .'"> <span>'.$author.', </span></p>
</div>';
if ($count%2 == 0)
{
$output .= '</div>';
}
$count++;
}
if ($count%4 != 1){
$output .= '</div>';
}
return $output;
}
I think you will have to pass 'posts_per_page' key in $args array. So your $args array will look like this
$args = array(
'post_type'=> 'testimonials',
'order' => 'ASC',
'posts_per_page' => '-1'
);
I am not sure about the $count variable you are using. I suppose you are trying to implement logic for pagination there but since your question is about showing all testimonials on single page, I won't get into that part.
Making changes in $args will return all testimonials objects.
I'm trying to modify a projects.php file which uses some loops and if statements to fetch projects based on there tags. I need to modify thee below code to only fetch projects with the tag new. Currently all the projects are being fetched and displayed.
Any help would be great. See code attached.
Thanks
<div id="projects" class="clearfix">
<?php $page_skills = get_post_meta($post->ID, "_ttrust_page_skills", true); ?>
<?php $skill_slugs = ""; $skills = explode(",", $page_skills); ?>
<?php if (sizeof($skills) >= 1) : // if there is more than one skill, show the filter nav?>
<?php $skill = $skills[0]; ?>
<?php $s = get_term_by( 'name', trim(htmlentities($skill)), 'skill'); ?>
<?php if($s) { $skill_slugs = $s->slug; } ?><?php endif;
$temp_post = $post;
$args = array(
'ignore_sticky_posts' => 1,
'posts_per_page' => 200,
'post_type' => 'project',
'skill' => $skill_slugs
);
$projects = new WP_Query( $args );
endif; ?>
<div class="wrap">
<div class="thumbs masonry">
<?php while ($projects->have_posts()) : $projects->the_post(); ?>
<?php
global $p;
$p = "";
$skills = get_the_terms( $post->ID, 'skill');
if ($skills) {
foreach ($skills as $skill) {
$p .= $skill->slug . " ";
}
}
?>
<?php get_template_part( 'part-project-thumb'); ?>
<?php endwhile; ?>
<?php $post = $temp_post; ?>
</div>
</div>
you should just need to add a field in your $args array :
$args = array(
'ignore_sticky_posts' => 1,
'posts_per_page' => 200,
'post_type' => 'project',
'skill' => $skill_slugs,
'tag' => 'new' // add this for the tag
);
$projects = new WP_Query( $args );
Let me know if it works :D
I tried with the code provided but it stil doesn't work...
and it started giving problems only after updating to wordpress 4.8.2
I copy also the code with the following parte where I close the foreach and the if ...
With Wordpress 4.7.6 works perfectly...
Any Idea?
Thanks a lot for the help!
<?php
$cat_array = array();
$args = array(
'post_type' => 'post',
'posts_per_page' => 9,
'ignore_sticky_posts' => 1
);
$my_query = null;
$my_query = new WP_Query($args);
if ($my_query->have_posts())
{
while ($my_query->have_posts()):
$my_query->the_post();
$cat_args = array(
'orderby' => 'none'
);
$cats = wp_get_post_terms($my_query->post->ID, 'category', $cat_args);
foreach($cats as $cat)
{
$cat_array[$cat->term_id] = $cat->term_id;
}
endwhile;
wp_reset_postdata();
}
if ($cat_array)
{
foreach($cat_array as $cat)
{
$category = get_term_by('ID', $cat, 'category');
$slug = get_term_link($category, 'category');
$id = $category->term_id;
$valore = "category_" . $id;
$colore = get_field('colore_categoria', $valore);
$immagine = get_field('immagine_ispirazione', $valore);
$testo_box = get_field('testo_box', $valore);
?>
<div class="colonna clearfix">
<a href="<?php echo $slug;?>">
<div class="box">
<img src="<?php echo $immagine?>" alt="italia">
<div class="overlay">
<p><?php echo $testo_box;?></p>
</div>
<div class="titolobox" style="background-color:<?php echo $colore;?>">
<h2><?php echo $category->name;?></h2>
</div>
</a>
</div>
</div>
<?php
}
}
wp_reset_query();
?>
Use 'ignore_sticky_posts' instead of 'caller_get_posts'.
You can refer to this page to update your query
I don't know your requirement but I think you didn't close the the foreach loop and if condition, and also I think that you need advance custom field value of category assigned to the post for that you should use
wp_get_post_categories( int $post_id, array $args = array() )
in your while loop. hope this help.
and you should use "ignore_sticky_posts" rather than "caller_get_posts"
use bellow code.
<?php
$cat_array = array();
$args = array(
'post_type' => 'post',
'posts_per_page' => 9,
'ignore_sticky_posts' => 1
);
$my_query = null;
$my_query = new WP_Query($args);
if ($my_query->have_posts())
{
while ($my_query->have_posts()):
$my_query->the_post();
$cat_args = array(
'orderby' => 'none'
);
$cats = wp_get_post_terms($my_query->post->ID, 'category', $cat_args);
foreach($cats as $cat)
{
$cat_array[$cat->term_id] = $cat->term_id;
}
endwhile;
wp_reset_postdata();
}
if ($cat_array)
{
foreach($cat_array as $cat)
{
$category = get_term_by('ID', $cat, 'category');
$slug = get_term_link($category, 'category');
$id = $category->term_id;
$valore = "category_" . $id;
$colore = get_field('colore_categoria', $valore);
$immagine = get_field('immagine_ispirazione', $valore);
$testo_box = get_field('testo_box', $valore);
}
}
?>
enjoy
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.
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'
));