here is my issue.
I am currently in a post (so in single.php). I got the only category that this post is in by using:
$category = get_the_category()[0];
Now, what I would like to be able to know is the current position of that post in that category and I need to be able to retrieve that position without searching through all pages of the category (for performances reasons).
E.g:
Let`s say my post is the 14th most recent post in its category, I want to get that value (14) so that I can then calculate on which category page it would be (knowing how many posts are displayed by page)
Thanks
If you know the number of posts/page ahead of time, you can just use SQL to do this.
Lets say you want to do 10 posts/page.
SELECT * FROM articles WHERE category_id = :categoryID LIMIT 0, 10;
That will give you the first 10 articles in a given category. If you see the articles for page two, you just modify the LIMIT
Related
I have created two Custom Post Types using JetEngine:
Place
Product
I have also enabled the JetReviews for the Place CPT. So, each Place can have multiple Products, and each Product can have multiple reviews.
I want to display the Place rating average based on their Product ratings. For instance: Product 1 has three reviews, the first one has 2 stars, the second one has 5 stars and the third one has 1 star. Therefore, the Product 1 rating is "2,66" stars.
Since I wasn't able to find a graphical solution to achieve this, I have created an SQL query using JetEngine QueryBuilder:
SELECT AVG({prefix}jet_reviews.rating)
FROM {prefix}jet_reviews
INNER JOIN {prefix}posts
ON {prefix}posts.id = {prefix}jet_reviews.post_id
INNER JOIN {prefix}jet_rel_default
ON {prefix}jet_rel_default.child_object_id = {prefix}posts.id
WHERE {prefix}jet_rel_default.parent_object_id = "{current_id}";
But here I didn't know how refer to the Place ID when viewing the Single Post, So I guessed it could be the same as the {prefix} (but this didn't work).
I have tried asking this on Crocoblock FB Community and its subreddit a while ago but hadn't been answered.
Does anyone know how to display the Product's average rating in each Place Single Post like I "explained" before?
I'm using MySQL and Php.
Here's the thing : I'm building a forum, with topics and posts.
Posts are displayed in a topic in different pages (let's say 10 posts/page).
My question is : if I ask to see a specific post in a topic, how can I know the page where the post is displayed ? (With the purpose to display the page required)
Should I calculate the number of pages then check each group of items of all possible pages or something like that ?
Thx for your help !
You could use Google Custom Search and let Google do all the hard work. I hear they're quite good for searching websites.
If you want something a little fancier you can pay for Google Site Search.
if your site is small and you want to try a really really simple solution why don't you explore MySQL Fulltext search?
That said the field, fields to search must be a Fulltext index. So you can grab a pair of text fields and some varchar fields and jointhem all in a Fulltex Index.
SELECT * FROM articles WHERE MATCH (title,body) AGAINST ('database' WITH QUERY EXPANSION);
But go and have a better look at MySQL manual!
Ok, I was eating and then... THE IDEA.
I guess a simple way to doing this is to count the number of items before the needed one (with ID or, in a better way, with Timestamps cause posts are ordered with those), and then calculate how many pages it takes.
Meaning, for example, if pages display 10 items, we have 24 items before our targeted one : ceil(24/10) = 3 item will be in page 3 !
Very simple when we think of it.
#Get the page displaying the selected item
#Step 1 : calculate how many items are before the selected one
$nbr_of_items = SELECT COUNT(post_id) FROM posts WHERE post_topic_id = :topic_id AND post_timestamp < :timestamp_of_selected_item
#Step 2 : do simple maths
$calcul = ceil($nbr_of_items / $nbr_of_items_displayed_in_a_page);
For example, we have 24 items before the selected one and pages display 10 items :
$calcul = ceil(24 / 10) = 3
Of course it could be updated (what if an other post has the same timestamp, etc) but it's a good start.
Using wordpress for a client (I wouldn't over wise).
On their home page I want to get the latest 4 posts for each of 4 different categories, I then want to get 3 additional posts that are latest from any category and not currently used above, followed by 5 with the greatest number of page views in the last 2 weeks.
All this is easy but to use wordpress get_posts would require 6 different calls to the database, can this be done with one instead?
The Procedure:
Get 4 latest posts from the category NEWS
Get 4 latests posts from the category POLITICS that were not already fetched from the first four (there is a possibility that posts are in both categories)
Get 4 latest posts from category FEATURES that were not already fetched
Get 4 latest posts from category COMMENT ... etc..
Get the 3 latest posts from any category that were not yet fetched
Get 5 posts with the greatest number of page views (stored in meta data under the name meta-page-views) that were not yet fetched
Is this possible without multiple queries.
Thanks,
There is no way to do this in one call with usual wordpress functions. It might be possible with different categories in one query but you can't have different sorting orders in one query.
If you really really have to use one query, you could pull for example the latest 100 posts, store them in an array and then use PHP to sort and filter the result to your categories. However, this won't work well with the page views. Also, a category that hasn't been posted within the last 100 posts wouldn't appear on your page.
I don't see a point in using one query only since it only creates problems. Even the most popular wordpress themes don't do that.
If your client is concerned about server load you should probably talk to them about caching.
I am trying to sort the information given to me by the API of an engineering journal. I have extracted the following information into a table:
ID (integer),
Journal Entry Name (Text),
Description (Text),
Page Length (integer),
Has Media (boolean)
Each "Journal Entry" has only one ID associated with it. Each ID also has other characteristics that are not returned by the API but that I want to use to sort. They are:
Category (Things like Econ, Math, Biology. Each ID can have more than one category)
Boolean values (Things like requiring special subscriptions)
I have created a second table in the following format:
ID (integer),
Category (text),
Boolean1 (bool),
Boolean2 (bool),
Boolean3 (bool)
Since each ID can have more than one category, when this occurs another row is added to this table. The idea being that any given row only has one ID and one category in it.
What I want to do is this:
Be able to find the top ten categories when it comes to
Highest Journal Entry (ID) count
Highest Total Page Length
Highest Journal Entry count where the "Has Media" boolean is true
Create a means of navigating like "pagination" where each page shows the nth results of the aforementioned top ten.
So if I chose to Highest Journal Entry count method, the first page would be show IDs, Names, and Descriptions of all the Journal entries in the category with the highest count.
My plan has been to create a new table where the numbers one through ten are in the first column, and then populate the second column with the top ten categories. Then I can use a process similar to pagination in which the nth page only shows the values with the corresponding category from the original value. However I can't seem to be able to make this top ten list/matrix, nor do I know if it there is a better way.
Unfortunately I am not a MySQL or PHP coder by trade, and have only gotten this far by lots and lots of googling. I have been completely unable to find any guides for a navigation method like the one I want. And since I don't know the proper terminology, I am just trying random google searches at this point.
Is this the best way to go about it? Or would it be better to create a third table of some sort? Is there perhaps an easier way to do this with something that can use the PHP and MySQL code I already wrote?
Not sure I really understand what you're going for here, but my best guess is that you probably want to combine your two initial tables and have category be a set rather than an individual term so you can have a single entry per unique ID.
Then you'd just need to write calls for each of your top ten finds as needed. Since each id can have an unknown number of categories I would start with a limit of 10 and then process the returns starting with the top match, grab its categories, if there are more than 10 grab the first 10, if there are less than 10 grab what there are, update the amount you're looking for (if there were 4 then you're now looking for 6), and move on to the next best match.
Maybe something like this:
categories = null;
$delimeter = ',';
$count = 1;
$top10 = array();
$result = mysql_query("
SELECT *
FROM table_name
ORDER BY page_length DESC
LIMIT 10");
while($row = mysql_fetch_array($result) && $count <=10)
{
$categories = $row['categories'];
$id = $row['id'];
$splitcontents = explode($delimeter, $catagories);
foreach($splitcontents as $category){
if (!in_array($catagory,$top10)){
$top10[$count] = array(
'category'=>$category,
'journal_id'=>$id
);
$count++;
}
}
}
Does anyone know how to display 3 previous posts on article page? Let's say I'm in article post page and would like to display previous 3 posts images (basing on the post I'm currently reading) below it's content, but not next posts images. I was trying to do this with setting up the offset in the db query but with no luck. Can someone please help me with this one? I would greatly appreciate any help or at least pointing me in the right direction.
That shouldn't be too hard, if you use an id field with auto increment values, you could do something like this:
SELECT id, name FROM articles WHERE id < {current_id} ORDER BY id DESC LIMIT 3;
Obviously, replace {current_id} with the id of the article you're currently reading.
After displaying the specific post do a new WP_Query to retrieve the 3 posts previous to the publication date of the displayed post. This documentation page describes how to query for posts with a relative date (e.g. the example Return posts from the last 30 days). Base the query on the publication time of the displayed post.
The example includes a way to supply a WHERE clause to the query, with add_filter(). I'm not sure, but I think you need to call remove_filter after doing the query, or that filter might be applied to other queries also.