Extending Wordpress search - php

I have a small problem in a wordpress project that I have worked in the past, the solution that I applied there for a custom plugin was to create a couple of tables for some projects that the admin has the ability to add. My mistake was to not make taxonomies and register them in wordpress. Now those projects must appear in the search results. My question is: How/where can I write some code to include a custom table in the search ( where 'q' LIKE %title% ) to apply for the internal search and let wordpress calculate the pagination and show the results? Thank you

Wordpress had capability to use native mysql query. So you just use the native query to search
$querystr = "
SELECT *
FROM posts
WHERE post_title LIKE %title%
ORDER BY posts.post_date DESC";
$pageposts = $wpdb->get_results($querystr, OBJECT);
foreach ($pageposts as $post):
echo $post->post_title;
endforeach;
About paging, count the row and do paging manually. I think you can do that.

Related

How to fetch posts with pagination feature in WP on a custom table?

I have a custom table that I created named cars_plugin and it has columns id, name, color, model
Now, I can get list of cars using
$sql = "SELECT note FROM cars_plugin WHERE ID = '$id'";
$query = $wpdb->get_results($sql, ARRAY_A);
foreach($query as $car){
// list cars here
}
The above code works as planned, and I could create a pagination for that, although it will be difficult, now my question is, if I have a custom table like that how do I query the table so I can be able to use the built-in Wordpress pagination functions?
This can't be done I think
I found no way to use WordPress default pagination for custom table. Cause the default pagination is initiated in WP_Query class which is curated or built or written only for querying posts from {wp-table-prefix}_posts table and may be joining other table related to posts.
So what is the solution ???
May be you can create your own class which will return you the result with pagination and this could be by extending WP_Query class. But this method will need a lot of modifications to manipulate the base class default methods. So I would prefer writing a separate new class for this than extending the WP_Query.
Another way would be straight PHP method. You can chunk the queried result and show them page by page. And I think it would be the easiest and smart way to do pagination for custom table in WordPress. For finding more on this method you can check those below links-
https://wordpress.stackexchange.com/questions/53194/wordpress-paginate-wpdb-get-results
http://www.walkswithme.net/wordpress-pagination-for-custom-tables
Hope that helps.

Retrieve Products By Category from OpenCart

How do I write a PHP snippet to read an OpenCart database to pull products by category and display it outside of an OpenCart framework? So for instance, I build this nice frontend website for widgets A and B, but then have a subdir called /cart where OpenCart is installed, and in my frontend website I load some kind of OpenCart library file and then read the database to get products by category (A and B). Or, perhaps you know the technique with direct SQL itself?
This question is different than this one because I'm trying to not build a new template in OpenCart, but use an API (or go direct to the database) to show products outside of OpenCart's application framework and in my own application framework.
I tried asking this over at the OpenCart forums, but oddly the mods deleted that post without explanation, and never told me why. So, I had no choice but to go digging for an answer from the code since the developer docs were poor for this kind of question. What I found was that the product catalog for OpenCart can be accessed by either looking at the class methods in the following file, or by loading that class file within your other web application. (As for how to load that class file, that will take some work to figure out -- I don't know yet.)
In your OpenCart 2.x directory, look for this file:
catalog/model/catalog/product.php
You'll find out from the SQL code how OpenCart gets a list of products and then how to get the product detail on a single product.
If you want the fastest way, building links like
https://your-shop.com.au/index.php?route=product/product&product_id=1579
you can get the product_id for the category "230" like this:
SELECT `oc_product`.`product_id`,`name`,`description`,`image`
FROM `oc_product`,`oc_product_description`
JOIN `oc_product_to_category`
ON `oc_product_to_category`.`product_id` = `oc_product_to_category`.`product_id`
WHERE
`oc_product_to_category`.`category_id` = '230'
AND `oc_product`.`status` = '1'
If you want SEO links use an array of product ids and process this query:
SELECT *
FROM `oc_url_alias`
WHERE
`query` LIKE '%PRODUCT_ID%'
ORDER BY `url_alias_id` DESC
Then you can do
https://your-shop.com.au/keyword
Here another query to get products from store 0, manufacturer 17, category 230 and posibility to build friendly SEO urls:
SELECT `oc_product`.`product_id`,`name`,`description`,`image`,`keyword`FROM `oc_product`
LEFT JOIN `oc_product_to_category`
ON `oc_product_to_category`.`product_id` = `oc_product`.`product_id`
LEFT JOIN `oc_product_description`
ON `oc_product_description`.`product_id` = `oc_product`.`product_id`
LEFT JOIN `oc_product_to_store`
ON `oc_product_to_store`.`product_id` = `oc_product`.`product_id`
LEFT JOIN `oc_url_alias`
ON (`oc_url_alias`.`query` LIKE CONCAT('%',`oc_product`.`product_id`,'%'))
WHERE `oc_product_to_category`.`category_id` = '230' AND `oc_product`.`status` = '1' AND `oc_product`.`date_available` <= NOW() AND `oc_product`.`manufacturer_id`='17' AND `oc_product_to_store`.`store_id` = '0'
ORDER BY `oc_product`.`product_id` DESC LIMIT 10

Display unique node using views in drupal

I am trying to display latest articles using views module. There are 12 article should be displayed per click of pagination excluding the duplicate entry. So, I tried as:
checked Advanced > Query settings > Distinct but no luck.
I have also tried as Views Distinct Settings > Filter/Aggregate this field > Filter Repeat, it works but number of articles are displayed less than 12.
Then, I tried to do it using the hook mymodule_views_query_alter but I do not understand how can I accomplish this task. I tried as:
if($view->current_display == "latest_article"){
$query->add_groupby('node.title');
}
But no solution. How can I override the query and inject DISTINCT function to get unique record.
Did you tried using Use aggregation option from Advanced section ? Hope this helps.

Fetch results based on parameter WordPress

I'm attempting to customize a wordpress theme. I've added custom field cp_job with form submission. This indicates if the ad is either Buyer(Buying services) or Seller(selling services), which is now complete.
Now I want to fetch results based on buyer or seller. In this wordpress theme classipress they have "Ads" on the side:
I simply want to fetch ads which have the attribute buyer or seller, which I assume is under the MySQL column cp_job. These ads will return the ad with all columns. Doing this in native PHP and MySQL would be easy but working around this theme/wordpress is way more complicated as I don't understand WordPress syntax.
You can use wordpress custom query to fetch record from any table.Like
global $wpdb;
// return array of object
$dataArr = $wpdb->get_results(" SELECT col FROM table WHERE cond = data ");
Then you can do foreach on records like below:
foreach( $dataArr as $data ){
echo $data->col;
}
Hope this helps ;)
Cheers!!!

Ordering Wordpress posts by id - exact order

it seems i can not find what i am looking for anywhere.
i am using the code below to show posts, using their ids.the problem is that i can't sort them the way i want - i need to order them exactly in order of the ids (currently it is sorting asc or desc.
i don't need that, i only want to show them exactly in the order of the ids)
<?php
$query_args = array('post_type'=>'post', 'category' => '3', 'numberposts'=>'50', 'post_status'=>'publish', 'include' => '100,124,122');
$query_posts = get_posts($query_args);
$last_query = end($query_posts);
foreach ($query_posts as $post) : setup_postdata($post);
any ideas?
I was halfway through writing an answer when I found wyfel's answer to a similar question on the WordPress stack exchange. Short of adding a global to store the IDs (rather than hard-coding them), it's what I planned to suggest (even down to the MySQL FIND_IN_SET function).

Categories