I used the following code to extract search results from "PHP Simple HTML DOM"
$url = "http://www.google.com/search? hl=en&safe=active&tbo=d&site=&source=hp&q={'$query'}&oq={'$query'}";
$html = file_get_html($url);
$linkObjs = $html->find('h3.r a');
but it returned maximum 10 results is anyway to retrieve 100 results from the search?
Thank you
Use num=100 parameter in the url like this one
$url = "http://www.google.com/search?hl=en&safe=active&tbo=d&site=&source=hp&num=100&q={'$query'}&oq={'$query'}";
Related
Getting Taxonomy term name from Taxonomy target ID:
I have a taxonomy term that accepts multiple values. It's rendered as a multiselectfield. I am trying to read the target ID of the field and figure the term name out of it using the below code in a preprocess function:
$granttype = $user_entity->field_user_grant_type->getValue();
foreach($granttype as $gt)
{
$granttype_name = \Drupal\taxonomy\Entity\Term::load($gt)->label();
}
dd($granttype_name);
$variables['grant_type'] = $granttype_name;
dd($granttype) shows the below output:
However, the foreach loop to figure out the term name is not working correctly.
dd($granttype_name) results as:
The website encountered an unexpected error. Please try again later.
TypeError: Illegal offset type in Drupal\Core\Entity\EntityStorageBase->load() (line 297 of core/lib/Drupal/Core/Entity/EntityStorageBase.php).
I am looping through the target ID and trying to get the term name. But it's not working. Any help pls?
UPDATE: I tried the below line of code:
$term = term::load($gt);
$name = $term->getName();
still no luck :( same error
Here is an example how to do this:
$grant_type = $user_entity->field_user_grant_type->entity;
if ($grant_type instanceof \Drupal\taxonomy\TermInterface) {
var_dump($grant_type->label());
}
If you have multiple referenced terms, use:
$grant_types = $user_entity->field_user_grant_types->referencedEntities();
foreach ($grant_types as $grant_type) {
var_dump($grant_type->label());
}
Explanation:
The generic way to get entity title is the Entity::label method
$term->label();
There is a helpful method Entity::referencedEntities to get relations.
First, you need to include
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\TermInterface;
Second, retrieve the value(s) stored in your field (field_user_grant_type) and store them in an array:
$myArray = array();
$granttype = $user_entity->get('field_user_grant_type')->getValue();
$granttype will now contain an array of arrays. Next, you need to gather the actual term IDs
foreach($granttype as $type){
$myArray[] = $type['target_id'];
}
Finally, loop through $myArray and fetch the term IDs stored there, and then use each ID to get its corresponding Term Name. Here, I store them in a new array called grantTypeNames
grantTypeNames = array();
foreach($myArray as $term_id){
$grantTypeNames[] = Term::load($term_id)->get('name')->value;
}
The array $grantTypeNames will now contain the term names you want. I hope that helps.
I wrote a function where two insert query have. One is executed and inserting data properly. But next one is not executing. And I cant check the value i want to insert if it is set or not. How to do the stuff? EXPERT's have a look kindly. My function is given below:
add_action( 'save_post', 'cs_product_save' );
function cs_product_save( $post_id ){
global $wpdb;
$cs_product_array = $_POST['cs_product'];
$cs_product_count = count($cs_product_array);
$event_start_date = $_POST['event_start_date'];
$event_end_date = $_POST['event_end_date'];
$event_start_time = $_POST['event_start_time'];
$event_end_time = $_POST['event_end_time'];
$event_all_day = $_POST['event_all_day'];
$event_phone = $_POST['event_phone'];
$event_location = $_POST['event_location'];
$event_map = $_POST['event_map'];
$table_cause_product = "wp_cause_woocommerce_product";
$table_event_info = "wp_cause_info";
for( $i=0; $i < $cs_product_count; $i++ ){
$wpdb->insert($table_cause_product,array(
'cause_ID'=>$post_id,
'product_ID'=>$cs_product_array[$i],
'status'=>'1'
),array('%d','%d','%d'));
}
$wpdb->insert($table_event_info,array(
'cause_ID'=>$post_id,
'event_start_date'=>$event_start_date,
'event_end_date'=>$event_end_date,
'event_start_time'=>$event_start_time,
'event_end_time'=>$event_end_time,
'event_all_day'=>$event_all_day,
'event_phone'=>$event_phone,
'event_location'=>$event_location,
'event_map'=>$event_map
),array('%d','%s','%s','%s','%s','%d','%s','%s','%d'));
}
I don't see any problem here with your code. But be sure to double check your code.
The issues my be with the name of your database table names. Are you sure that $table_cause_product and $table_event_info holds the actual name of the tables? I would recommend to use $wpdb->prefix instead of harcoding table names.
In my case I would check the function in several parts.
Check the $_POST actually holds the data I want.
Use $result = $wpdb->insert( $table, $data, $format ); in all cases for debug purpose as the $result would hold the result of the operation. If its is false then I would be sure that there is definitely something wrong with the operation.
Finally I would use a wp_die() (though its not a standard way to do, but it suffices my purpose) so that I can see the dumped variable data.
One major issue you might face with your code that if the post is edited after saving then it might insert another row for same post data. Again if the post is being autosaved, you need some safeguard. I would recommend a where clause here to check the row already exists or not. If exists then you can simply update the row, or else insert the data.
Hope this might help you.
Is it possible to fetch a post with content under 140 characters or 25 words ?
if possible how to do it
here is my random post code
// Random post link
function randomPostlink(){
$RandPostQuery = new WP_Query(array('post_type'=>array('tip'),'posts_per_page' => 1,'orderby'=>'rand'));
while ( $RandPostQuery->have_posts() ) : $RandPostQuery->the_post();
echo the_permalink();
endwhile;
wp_reset_postdata();
}
Character count is easy, you can just add the condition AND CHAR_LENGTH(post_content) < 140 to your where clause.
Word count is more difficult because there is no built in MySQL function for counting words. You can find simple solutions that don't work in every use case as well as complete solutions that use stored functions. I'll use a simple solution for the sake of example.
What you need to do is add a filter to the where clause and apply your additional conditions there:
add_filter( 'posts_where', 'venki_post_length_limit' );
function venki_post_length_limit($where = '') {
remove_filter( 'posts_where', 'venki_post_length_limit' );
$where .= ' AND (
CHAR_LENGTH(post_content) < 140 OR
(LENGTH(post_content) - LENGTH(REPLACE(post_content, ' ', ''))+1) < 25
) ';
return $where;
}
Notice that I remove the filter as soon as the function is called. This is so you don't apply this same condition to every query.
You should also be aware that both of those conditions are costly compared to a simple lookup on a column value (especially the word count). Neither can utilize indexes. If you have a large number of posts you may run into performance issues if you're running this query frequently. A better solution might be to calculate the word and character count when the post is created/updated and store that as meta data.
I have a function where i do theses 2 requests :
$in = $this->em->getRepository("QueensBundle:CaisseMvtArticle")
->findOneBy(array('ville_id'=>$lieu,'nature'=>'out','scoope'=>'central'));
$out = $this->em->getRepository("QueensBundle:CaisseMvtArticle")
->findOneBy(array('ville_id'=>$lieu,'nature'=>'out','scoope'=>'ville'));
when i execute it and try to read the second request result, the variable $out has the same data as the first ($in). when i remove the first query($in) , the $out variable has the expected datas. I don't understand this. What can i do to get the two requests executed and get good results for each one ?
I am a little unsure how to do a WP_Query to search a meta value of my posts. The meta data includes a persons height. So valid entries would be 5'9" or 4'11" and I need to do a Compare-Between.
To make it a bit clearer: I have a filter page. So the user can select the height between 4'0" - 5'9 or 5'9" - 5'11"
The problem is I have the ' and " symbols. I can remove these. But then it will be searching between 59 and 511 which is not going to work.
Anyone know of a work around?
Save the values in inches.
You could then set up a WP_Query or a $wpdb query to fetch your results.
Whenever you are displaying the values on the front end, you convert them back to feet and inches.
update:
function convert_to_feet( $input){
$feet = (int) ($input/12);
$inches = $input%12;
return $feet . "'" . $inches . '"';
}
echo convert_to_feet(71);