This topic contains 7 replies, has 2 voices, and was last updated by mashwebdesign 9 years, 9 months ago.
- AuthorPosts
- December 1, 2014 at 4:58 pm #3061
Hi, Sorry site is currently on localhost so cant provide login details.
I love the functionality available in your plugin however i have one issue. I want to customise the results my self. For example i assumed there would be a wp_query call somewhere in your files? I was going to go inside the loop and make custom calls to different custom fields such as:
get_field(‘delay_type’) etc etc etc
Can you please help me on how to do this?
Adam
December 1, 2014 at 5:09 pm #3062Hi Adam!
The plugin uses an entirely different method to parse results than the built in queries. I do not recommend modifying the code, as you will loose the changes if you upgrade the plugin.
There are however filters and actions available, that you can use to customize the results or add/remove new ones.
Here is a knowledgebase article about adding category titles to the result titles: https://wp-dreams.com/knowledge-base/showing-the-category-titles-in-the-result-title/
It’s a good starting point to understand how the results structure looks like.Based on the example above you can replace the ‘asp_add_category_titles’ function body with something like:
function asp_add_category_titles( $pageposts ) { var_dump($pageposts); return $pageposts; }
..and then examining the xhr/ajax request you will see the structure of the $pageposts variable, which is an array of objects, where the objects are the results.
Another example: https://wp-dreams.com/knowledge-base/showing-the-post-type-name-in-post-title/
The full action and filter list is available in the plugins main directory in the actions.txt and filters.txt files.
So using this filter you can do whatever you want with the result list. It requires a bit PHP knowledge though.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
December 1, 2014 at 5:17 pm #3066Hi mate,
Cheers my php is average so may need some assistance. I have put the code you wrote, into my themes functions.php but it isn’t changing the search results at all like nothing is taking affect? Any ideas?
add_filter( ‘asp_pagepost_results’, ‘asp_add_category_titles’, 1, 1 );
function asp_add_category_titles( $pageposts ) {
var_dump($pageposts);
return $pageposts;}
December 1, 2014 at 5:25 pm #3067As I said before it’s just a guidance.
It changes nothing, but you can check the XHR request with your browser developer tools, where besides the regular request you will see the dumped structure of the $pageposts variable.
If you want to change the results, you need to change the content of the $pageposts variable, but before you can do that, you need to understand what’s in there, that’s why I suggested that function.
I highly recommend reading about filters and actions first before you proceed: http://codex.wordpress.org/Plugin_API
I don’t know what you are trying to achieve exactly, so that’s why I suggested the guidelines.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
December 1, 2014 at 5:27 pm #3068Cheers mate, thought it would var_dump() it on the page. TBH the code i need to add the $pageposts is something along the lines below. I have a whole host of different custom fields that need to be brought out into the results, do you think it is possibly:
echo ‘<div class=”further-info”>’;
if(get_field(‘from-to-or-at’) == ‘from-to’):
echo ‘<p class=”fromto”><i class=”fa fa-road”></i>From: ‘ . get_field(‘from’) . ‘ To: ‘. get_field(‘to’) .'</p>’;
elseif(get_field(‘from-to-or-at’) == ‘at’):
echo ‘<p class=”fromto”><i class=”fa fa-road”></i> AT:’ .get_field(‘at’) . ‘</p>’;
endif;
echo ‘<p class=”delay-type”><i class=”fa fa-car”></i> ‘ . get_field(‘delay_type’) . ‘</p>’;
$countin = 0;
foreach($incidenttype as $value):
$countin++;
if($countin == 1):
echo ‘<p><i class=”fa fa-cogs”></i> Incident Type: ‘ . $value . ‘</p>’;
else:
echo ‘<p>’ . $value . ‘</p>’;
endif;
endforeach;
echo ‘<p><i class=”fa fa-clock-o”></i> ‘ . get_the_time() . ‘</p>’;
if(get_field(‘avoid’)):
echo ‘<p class=”avoid-p”><i class=”fa fa-warning”></i> Avoid</p>’;
endif;
echo ‘</div>’;
if(get_field(‘avoid’)):
echo ‘<i class=”fa fa-warning-large fa-warning”></i>’;
endif;
December 1, 2014 at 5:46 pm #3069Alright mate, starting to understand a little bit more now.
I am trying to concatenate the custom fields onto $pageposts->content see below:
add_filter( ‘asp_pagepost_results’, ‘asp_show_the_post_type’, 1, 1 );
function asp_show_the_post_type( $pageposts ) {
foreach ($pageposts as $k=>$v) {// Get the post categories
$html .= ‘<div class=”further-info”>’;if(get_field(‘from-to-or-at’) == ‘from-to’):
$html .= ‘<p class=”fromto”><i class=”fa fa-road”></i>From: ‘ . get_field(‘from’) . ‘ To: ‘. get_field(‘to’) .'</p>’;
elseif(get_field(‘from-to-or-at’) == ‘at’):
$html .= ‘<p class=”fromto”><i class=”fa fa-road”></i> AT:’ .get_field(‘at’) . ‘</p>’;
endif;
$html .= ‘<p class=”delay-type”><i class=”fa fa-car”></i> ‘ . get_field(‘delay_type’) . ‘</p>’;
$countin = 0;
foreach($incidenttype as $value):
$countin++;
if($countin == 1):
$html .= ‘<p><i class=”fa fa-cogs”></i> Incident Type: ‘ . $value . ‘</p>’;
else:
$html .= ‘<p>’ . $value . ‘</p>’;
endif;
endforeach;
$html .= ‘<p><i class=”fa fa-clock-o”></i> ‘ . get_the_time() . ‘</p>’;
if(get_field(‘avoid’)):
$html .= ‘<p class=”avoid-p”><i class=”fa fa-warning”></i> Avoid</p>’;
endif;
$html .= ‘</div>’;
$cats = “”;
// Modify the post title
$pageposts[$k]->content = $pageposts[$k]->content . $html;}
var_dump($pageposts);
return $pageposts;
}It works however it doesn’t get the custom fields? Any idea why it doesn’t?
Cheers
December 1, 2014 at 5:53 pm #3070Yes. I see you are using the ACF plugin. In this case I think you need the extended version of the function, because you are not in the main loop
so instead of:
get_field('fieldname')
use the:
get_field('fieldname', $pageposts[$k]->id)
it might work.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
December 1, 2014 at 6:12 pm #3072Cheers mate, should be able to get this to work. May i quickly pick your brains.
Okay so i understand these are preset filters that i can hook into. And that they are in a object that i can access. But where in the files are they actually declared, looped through and echo’d out.
Out of curiosity?
- AuthorPosts
You must be logged in to reply to this topic.