Count meta_keys (Wordpress) - php

Is there a simple way to count how often a meta_key exists in the database like wordpress do with:
$n_post = wp_count_posts();
Anybody can help?

Check last rows in a table which are associated with specific user/post, Take that ID and count meta_keys on the bases of that ID, Hope it will return all the meta_keys...

Related

display wp_postmeta meta_value in wordpress woocommerce

please help a new member
THE ISSUE:
I have a POS on my WC and it doesn't display sequential orders numbering, so I am using other plugin to achieve this. I need to display the value of a meta_value from my database in the order receipt (printable ticket) after the order is placed. So, the meta_value I need to display on the POS's tickets is the sequential ticket number created by the other plugin.
Gladly this other plugin creates the sequential number in the database from where I hope I can retrieve it somehow.
This meta_value for example "T00001...T00009" has the meta_key = "_order_number" and it's within wp5o_postmeta table of my sql database.
I believe it must be easy for you guys...
Thank you so much
Image of the data
use this filter and implement your post meta
wordpress/wp-content/plugins/woocommerce-openpos/includes/front/Front.php
line 4683
$result['data'] = apply_filters('op_get_next_order_number_info',$order_number_info);

Laravel exclude voted posts

I have two tables: posts and votes, and I want to display only one post on home page, and then displaying them in random order. But I want to exclude posts that are already upvoted. So I want to loop only between not-voted posts. I am using Laravel 5.4
Structure for posts table:
id
user_id
...
Structure for votes table:
id
user_id
post_id
...
I just need a query for excluding posts, everything else I already have written.
Thanks
Assuming you have votes relationship in your Post model (I assume you use Eloquent) you can use:
$posts = Post::doesntHave('votes')->get();
to get posts that don't have any votes.
Obviously it might be not a good idea to run get() here as you will get all posts that don't have votes - and what in case there are 100 000 posts matching this criteria? You should probably only get some of them and then choose one random for example:
$posts = Post::doesntHave('votes')->latest()->take(100)->get();

Selecting most recent result from MySQL where there are more than 1 rows matching ID

I'm using wordpress with some custom fields, to cut this short, im echoing out all rows that have the meta_key of post_author that arent empty and grouping likewise names together otherwise i might get Pete, Steve, Pete - when all i need is Pete, Steve. Below is the SQL that makes that work
$wpAuthors = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT(meta_value)
FROM $wpdb->postmeta WHERE meta_value !='' AND meta_key = 'post_author'
ORDER BY meta_key ASC"));
That works great. But what if i change the author FROM pete TO steve? Of course wordpress makes a duplicate entry for that post id, with a different Custom field row id. (Post ID on both rows would be something like 1100 while entry rows might be 3000 and 3001). Now the issue here is that my script still works, because both Pete and Steve are entered into the database in the correct column, but what i need it to do now is to select the most recent copy of that post id row to get the most recent author, does that make sense? I hope so!
define('WP_POST_REVISIONS', false); Add this variable in to wp-config.php, it will prevent wordpress to create post revisions.
After that install this plugin http://wordpress.org/plugins/wp-optimize/screenshots/.
Now remove all post revisions using above wp-optimize plugin.

Non standard sorting and navigation using MySQL and PHP

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++;
}
}
}

filtering number of rows with similar content in php mysql

I'm trying to have mysql outputting a list of article categories.
there are lots of articles, each with preset categories that are stored in mysql. But many articles have the same category, so my list get very long with similar category results.
My idea is that if the user has posted 1 post in a category, the category gets listed. but this needs to understand that the category should just be listed once even if the user has posted multiple times in that specific category. How can i do this?
This is what i got, but not working:
foreach( $result as $row ) {
if($result>1){
$kategorilink= "{$row['kategori']}";
echo $kategorilink;
}
}
Try SELECT DISTINCT * FROM .... This will give you each different value only once.
modifying mysql data in php is not a good idea, you can select disting categories from mysql like
select distinct(category) from article where full_name='$safe_name'
or you can add group by clause to your query to group your result according to categories
select * from article where full_name='$safe_name' group by (category)
if you want to check number of results you can use mysql_num_rows() like
if (mysql_num_row($result) > 1){ //code here}

Categories